取消实例化 [英] Cancelling instantiation

查看:73
本文介绍了取消实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了在执行对象的

构造函数时抛出异常,有没有办法以这样的方式取消对象的创建

的方式它留下对null或undefined的引用。这将使

成为简单的测试:


var ref = new someObject();


if(ref ){

//对象创建成功

}


我知道可以使用状态标志代替,但是我拥有的对象记住
如果传递给它的参数不正确,那么
应该不存在(有一个好的理由,请相信我)。


[OT - 一般例外(包括其他语言)]


有些人似乎在

对象构造期间抛出异常问题表示失败,而宁愿使用州

标志。无论如何都有任何想法?


[/ OT]


顺便说一下,不可能有受保护的[1]成员在JavaScript中,

是吗?惭愧......


非常感谢,

迈克

[1]我的意思是受保护,而不是私人。


-

Michael Winter
M. ******@blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)

Other than throwing an exception during the execution of an object''s
constructor, is there any way to cancel creation of the object in such a
way that it leaves a reference to it null or undefined. That would enable
the simple test:

var ref = new someObject();

if( ref ) {
// Object created successfully
}

I know one could use state flags instead, but the object I have in mind
shouldn''t exist if the arguments passed to it are incorrect (there is a
good reason, trust me).

[OT - Exceptions in general (including other languages)]

Some people seem to have issues concerning throwing an exception during
object construction to signify failure and instead prefer to use state
flags. Any thoughts either way?

[/OT]

By the way, it''s not possible to have protected[1] members in JavaScript,
is it? Shame...

Many thanks,
Mike
[1] I do mean protected, not private.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

推荐答案

Michael Winter写道:
Michael Winter wrote:
除了在执行对象的构造函数期间抛出异常,有没有办法取消在
这样一种方式,它将对它的引用保留为null或undefined。


不,唯一的情况是调用构造函数的内部[[Construct]]

方法不会导致创建的返回 -

this - object是对[[Call]]方法的内部调用返回Type对象的

值(而不是typeof" object")。 br />
这将启用简单测试:


也许你可以返回一个可能(可能间接)的对象

转换为布尔值以一种通常无法实现的方式表示错误

使用构造的对象。 EG: -


函数Const(arg1){

if(!arg1){

return(new String(") ));

}

}


var a = new Const(false);


if(String(a)){

alert(''Object construct'');

} else {

alert(''空字符串对象返回'');

}


- 但重要的是对象不要超载

Object.prototype.toString方法,以便(永远)返回一个

空字符串。或者: -


函数Const(arg1){

if(!arg1){

return(new Number(0) );

}

}


var a = new Const(false);


if(Number(a)){

alert(''Object construct'');

} else {

alert( ''返回空字符串对象'');

}


< snip> [OT - 一般情况下的例外情况(包括其他语言)]

有些人似乎在对象构造过程中出现了抛出异常的问题,表示失败,而更喜欢使用状态标志。有什么想法?


对于跨浏览器客户端脚本,try-catch尚未使用

,因为当前使用的较旧浏览器将它们视为

保留字,在代码加载时生成语法错误,甚至从未尝试执行它。
尝试执行它。 (这是投入如此多努力的原因之一,因为
被用于测试环境,所以不会抛出异常并且

没有必要处理它们。)

顺便说一句,它不可能在JavaScript中拥有受保护的[1]成员,是吗?羞耻......
< snip> [1]我的意思是保护,而不是私人。
Other than throwing an exception during the execution of an object''s
constructor, is there any way to cancel creation of the object in
such a way that it leaves a reference to it null or undefined.
No, the only circumstances where a call to the internal [[Construct]]
method of a constructor will not result in the return of the created -
this - object is when the internal call to the [[Call]] method returns a
value of Type object (as opposed to typeof "object").
That would enable the simple test:
Maybe you could return an object that can be (probably indirectly)
converted to boolean false in a way that could not normally be achieved
with the constructed object. EG:-

function Const(arg1){
if(!arg1){
return (new String(""));
}
}

var a = new Const(false);

if(String(a)){
alert(''Object constructed'');
}else{
alert(''Empty string object returned'');
}

- but it would be important that the object not overload the
Object.prototype.toString method in such a way as to (ever) return an
empty string. Or:-

function Const(arg1){
if(!arg1){
return (new Number(0));
}
}

var a = new Const(false);

if(Number(a)){
alert(''Object constructed'');
}else{
alert(''Empty string object returned'');
}

<snip> [OT - Exceptions in general (including other languages)]

Some people seem to have issues concerning throwing an exception
during object construction to signify failure and instead prefer
to use state flags. Any thoughts either way?
For cross-browser client-side scripting try-catch cannot yet be used
because the older of the browsers currently in use regard them as
reserved words, generate syntax errors as the code loads and never even
attempts to execute it. (Which is one of the reasons that so much effort
gets put into testing the environment, so exceptions are not thrown and
there is no need to handle them.)
By the way, it''s not possible to have protected[1] members in
JavaScript, is it? Shame... <snip> [1] I do mean protected, not private.




他们可以被模拟,但努力,引入的开销和代码膨胀

可能否定任何优势。


Richard。



They can be emulated but the effort, introduced overheads and code bloat
would probably negate any advantage.

Richard.


Richard Cornford写道:< c2 ********** *********@news.demon.co.uk>

< snip>
Richard Cornford wrote: <c2*******************@news.demon.co.uk>
<snip>
....或者: -

函数Const(arg1){
if(!arg1){
return(new Number(0));
}
}

var a = new Const(false);

if(Number(a)){
alert(''Object construct'');
} else {
alert(''返回空字符串对象');
}
... . Or:-

function Const(arg1){
if(!arg1){
return (new Number(0));
}
}

var a = new Const(false);

if(Number(a)){
alert(''Object constructed'');
}else{
alert(''Empty string object returned'');
}



< snip>


该版本将无效,因为该对象将转换为NaN,

将转换为布尔值false。除非对象

故意重载valueOf方法,否则返回的Number

对象非零并且测试的逻辑反转。可能更好

只是为了使用String版本。


Richard。


<snip>

That version will not work as the object would be converted to NaN,
which would than convert to boolean false. Unless maybe the object
deliberately overloaded the valueOf method, or the returned Number
object was non zero and the logic of the test reversed. Probably better
just to use the String version.

Richard.


< snip>
<snip>
顺便说一句,它不可能在JavaScript中拥有受保护的[1]成员,是吗?惭愧......
By the way, it''s not possible to have protected[1] members in
JavaScript, is it? Shame...




不,这是关于Javascript的谬论之一。 Javascript

支持闭包,因此支持受保护/私有成员。请参阅:

http://www.crockford。 com / javascript / private.html


这篇关于取消实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆