使用DontEnum属性定义属性 [英] Defining a property with the DontEnum attribute

查看:79
本文介绍了使用DontEnum属性定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部 -


我正在努力解决一个问题,我认为解决方案将是* b $ b to * cheat *;但我不介意这样做。背景是:


给定一个对象构造函数和一个实例


SampleObj = function(){

this.prop = 1;

}

obj = new SampleObj();

obj有一个可枚举的属性:''prop'';以及几个内置的,
不可枚举的属性:''toString'',''hasOwnProperty'',

''propertyIsEnumerable''等等。如果我重新定义其中任何一个,例如:


SampleObj.prototype.toString = function(){return''[SampleObj]''};


然后obj仍然只有一个可枚举的属性;重新定义

toString不会影响它的DontEnum-ness。


我希望能够创建这样的属性来自JavaScript。特别是

,在旧版本的Safari中,其中一些必需的属性

没有实现(hasOwnProperty,isPrototypeOf,propertyIsEnumerable,

& toLocaleString)。如果我自己定义它们:


Object.prototype.toLocaleString = Object.prototype.toString;

[等等]


然后它们是可枚举的,这会导致代码的问题,例如


for(var ob in obj)...


I在Safari中获取一组不同的枚举属性而不是(例如)

Mozilla;并且必须专门检查我自己定义的那些,

以使代码在每个浏览器上正常工作。这很棘手

(考虑到旧的Safari版本也未能实现

toExponential,toFixed和toCrecision for Number对象)。


设置对象的DontEnum和ReadOnly属性

属性的能力会很好;但是(afaik)没有办法这样做。所以

我正在寻找一个后门在Safari中允许这样做。


帮助任何人?

Howard Jess

All --

I''m trying to solve a problem for which I think the solution will be
to *cheat*; but I don''t mind doing so for this case. The background is:

Given an object constructor, and an instance

SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: ''prop''; and several builtin,
non-enumerable properties as well: ''toString'', ''hasOwnProperty'',
''propertyIsEnumerable'', etc. If I redefined any of those, e.g:

SampleObj.prototype.toString = function() {return ''[SampleObj]''};

then obj still has only one enumerable property; the redefinition of
toString doesn''t affect its "DontEnum"-ness.

I''d like to be able to create such a property from JavaScript. In
particular, in older versions of Safari, some of those required properties
aren''t implemented (hasOwnProperty, isPrototypeOf, propertyIsEnumerable,
& toLocaleString). If I define them myself:

Object.prototype.toLocaleString = Object.prototype.toString;
[etc.]

then they are enumerable, which causes problems for code like

for (var prop in obj) ...

I''ll get a different set of enumerated properties in Safari than in (e.g.)
Mozilla; and have to specifically check for those that I defined myself,
to make code work appropriately on each browser. This gets tricky
(considering that the old Safari versions also failed to implement
toExponential, toFixed and toPrecision for Number objects).

The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to do so. So
I''m looking for a "backdoor" in Safari that would allow this.

Help anyone?
Howard Jess

推荐答案



霍华德杰斯写道:

Howard Jess wrote:
全部 -

我正在努力解决我认为解决方案的问题是*作弊*;但我不介意这样做。背景是:

给定一个对象构造函数和一个实例

SampleObj = function(){
this.prop = 1;
}
obj = new SampleObj();

obj有一个可枚举的属性:''prop'';还有几个内置的,不可枚举的属性:''toString'',''hasOwnProperty'',
''propertyIsEnumerable''等等。如果我重新定义了其中任何一个,例如:

SampleObj.prototype.toString = function(){return''[SampleObj]''};

然后obj仍然只有一个可枚举的属性;重新定义
toString并不会影响它的DontEnum-ness。

我希望能够从JavaScript创建这样的属性。特别是在旧版本的Safari中,其中一些必需的属性没有实现(hasOwnProperty,isPrototypeOf,propertyIsEnumerable,
&toLocaleString)。如果我自己定义它们:

Object.prototype.toLocaleString = Object.prototype.toString;
[等等]
那么它们是可枚举的,这会导致问题代码如

for(var ob in obj)...

我会在Safari中获得一组不同的枚举属性,而不是(例如)
Mozilla;并且必须专门检查我自己定义的那些,
使代码在每个浏览器上正常工作。这很棘手
(考虑到旧的Safari版本也未能实现对Number对象的指数,toFixed和toPrecision)。

设置DontEnum和ReadOnly属性的能力对象
属性会很棒;但是(afaik)没有办法这样做。所以
我正在寻找一个后门在Safari中允许这样做。

帮助任何人?
All --

I''m trying to solve a problem for which I think the solution will be
to *cheat*; but I don''t mind doing so for this case. The background is:

Given an object constructor, and an instance

SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: ''prop''; and several builtin,
non-enumerable properties as well: ''toString'', ''hasOwnProperty'',
''propertyIsEnumerable'', etc. If I redefined any of those, e.g:

SampleObj.prototype.toString = function() {return ''[SampleObj]''};

then obj still has only one enumerable property; the redefinition of
toString doesn''t affect its "DontEnum"-ness.

I''d like to be able to create such a property from JavaScript. In
particular, in older versions of Safari, some of those required properties
aren''t implemented (hasOwnProperty, isPrototypeOf, propertyIsEnumerable,
& toLocaleString). If I define them myself:

Object.prototype.toLocaleString = Object.prototype.toString;
[etc.]

then they are enumerable, which causes problems for code like

for (var prop in obj) ...

I''ll get a different set of enumerated properties in Safari than in (e.g.)
Mozilla; and have to specifically check for those that I defined myself,
to make code work appropriately on each browser. This gets tricky
(considering that the old Safari versions also failed to implement
toExponential, toFixed and toPrecision for Number objects).

The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to do so. So
I''m looking for a "backdoor" in Safari that would allow this.

Help anyone?




if(&myName中的propertyName)

给出[true]如果propertyName出现在对象

本身或其原型链ab obo中的任何位置。


if(myObject.hasOwnProperty( " propertyName"))

只有在对象本身中显示propertyName时才会给出[true]

(原型链被忽略)


所以这是一个相当简单的任务。但如果在旧的Safari之下你的意思是

任何低于2.0的东西然后我非常反映这些方法是

赠送它们 - 即使它们是 - 它们将作为
$ b工作$ b他们应该。

Safari 1.x系列几乎无法识别HTML本身,什么

hasOwnProperty,算了吧! :-)

无论如何你可以尝试。



if ("propertyName" in myObject)
gives you [true] if propertyName is presented either in the object
itself or anywhere in its prototype chain ab obo.

if (myObject.hasOwnProperty("propertyName"))
gives you [true] only if propertyName is presented in the object itself
(prototype chain is disregarded)

So it is rather simple task. But if under "older Safari" you mean
anything below 2.0 then I dubt very much that these methods are
presented themselve - and even if they are - that they will work as
they should.
Safari 1.x series can barely recognize HTML itself, what
hasOwnProperty, forget it! :-)
You may try anyway.


Howard Jess写道:
Howard Jess wrote:
SampleObj = function(){
this.prop = 1;
}
obj = new SampleObj();

obj有一个可枚举的属性:''prop '';还有几个内置的,不可枚举的属性:''toString'',''hasOwnProperty'',
''propertyIsEnumerable''等等。如果我重新定义了其中任何一个,例如:

SampleObj.prototype.toString = function(){return''[SampleObj]''};

然后obj仍然只有一个可枚举的属性;重新定义
toString并不会影响它的DontEnum-ness。

我希望能够从JavaScript创建这样的属性。


你不能这样做。

[...]
能够设置对象的DontEnum和ReadOnly属性
属性本来会很棒;但是(afaik)没有办法这样做。所以我正在寻找一个后门在Safari中允许
这个。
SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: ''prop''; and several builtin,
non-enumerable properties as well: ''toString'', ''hasOwnProperty'',
''propertyIsEnumerable'', etc. If I redefined any of those, e.g:

SampleObj.prototype.toString = function() {return ''[SampleObj]''};

then obj still has only one enumerable property; the redefinition of
toString doesn''t affect its "DontEnum"-ness.

I''d like to be able to create such a property from JavaScript.
You cannot do this.
[...]
The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to
do so. So I''m looking for a "backdoor" in Safari that would allow
this.




至于DontEnum:


Object.prototype.toLocaleString.dontEnum = true;


for(var foo in obj)

{

if(!obj [foo] .dontEnum)< br $>
{

// ...

}

}


应该到处工作,不仅仅是在Safari中工作。


至于ReadOnly,没有办法在JavaScript中实现

相似的东西。 2.0 / JScript< 7.0 / ECMAScript< 4.

HTH


PointedEars



As for DontEnum:

Object.prototype.toLocaleString.dontEnum = true;

for (var foo in obj)
{
if (!obj[foo].dontEnum)
{
// ...
}
}

Should work everywhere, not only in Safari.

As for ReadOnly, there is no way to implement something
alike in JavaScript < 2.0/JScript < 7.0/ECMAScript < 4.
HTH

PointedEars


Thomas''PointedEars''Lahn写道:
Thomas ''PointedEars'' Lahn wrote:
SampleObj = function(){
this.prop = 1;
}
obj = new SampleObj();

obj有一个可枚举的属性:''prop'';以及几个内置的,不可枚举的属性:''toString'',''hasOwnProperty'',
''propertyIsEnumerable''等等。如果我重新定义了其中任何一个,例如:
[snip]
我希望能够用JavaScript创建这样的属性。
你不能这样做。
SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: ''prop''; and several builtin,
non-enumerable properties as well: ''toString'', ''hasOwnProperty'',
''propertyIsEnumerable'', etc. If I redefined any of those, e.g: [snip]
I''d like to be able to create such a property from JavaScript.
You cannot do this.




是的,正如我在这里所说:



Yes, as I say right here:

[...]
设置对象
属性的DontEnum和ReadOnly属性的能力会很好;但是(afaik)没有办法这样做。所以我正在寻找一个后门在Safari中允许
这个。
[...]
The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to
do so. So I''m looking for a "backdoor" in Safari that would allow
this.



通过后门,我的意思是一些非正统的,无证的钩子进入Safari的

JS发动机。据我所知,这不符合规格。

至于DontEnum:

Object.prototype.toLocaleString.dontEnum = true;

(obj中的var foo)
{
if(!obj [foo] .dontEnum)
{
// ...
}
}

应该在任何地方工作,不仅仅是在Safari中。


By "backdoor", I mean some unorthodox, undocumented hook into Safari''s
JS engine. I understand that this is not possible per the spec.
As for DontEnum:

Object.prototype.toLocaleString.dontEnum = true;

for (var foo in obj)
{
if (!obj[foo].dontEnum)
{
// ...
}
}

Should work everywhere, not only in Safari.




是的,当然,如果我想更改* every *" for ..在"在我处理的每一段
代码中循环;但这仍然不能令人满意;

dontEnum您建议的财产本身现在是一个可枚举的财产

toLocaleString。


重复:我正在寻找一个后门,作弊 ;,挂钩 (发明你的

自己的术语)允许我定义的财产是不可枚举的。


hj



Yes, of course, if I want to change *every* "for .. in" loop in every
piece of JavaScript code I deal with; but this is still unsatisfactory;
the "dontEnum" property you propose is itself now an enumerable property
of toLocaleString.

To repeat: I''m looking for a "backdoor", "cheat", "hook" (invent your
own term) to allow a property that I define to be non-enumerable.

hj


这篇关于使用DontEnum属性定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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