A
Asen Bozhilov
__proto__ property in some implementation of ECMA-262 is mimic of
internal [[Prototype]] property. More information can be found on:
<URL: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/proto
/>
In SpiderMonkey implementation every native object has own property
`__proto__`, which refer next object in prototype chain.
var obj = {};
window.alert(obj.__proto__ === Object.prototype); //true
The next object in prototype chain of object referred by `obj' is
Object.prototype. The prototype chain of that object looks:
obj -> Object.prototype -> null
window.alert(obj.hasOwnProperty('__proto__')); //true
window.alert(obj.propertyIsEnumerable('__proto__')); //false
`__proto__' is own property of that object and has internal attribute
{DontEnum}, which mean that property cannot be enumerate during `for-
in` loop.
window.alert(delete obj.__proto__); //false
Even {DontEnum} attribute that property has and {DontDelete} internal
attribute.
But if I assign primitive null value on that property in SpiderMonkey
some of the described things above are changed.
var obj = {__proto__ : null};
window.alert(Object.prototype.propertyIsEnumerable.call(obj,
'__proto__')); //false
window.alert(delete obj.__proto__); //true
`__proto__' still have {DontEnum} attribute but do not have
{DontDelete} yet. That is not the big problem. The problem is
`__proto__' yet is not mimic of internal [[Prototype]]. The previous
state of prototype chain cannot be restored.
var obj = {};
obj.__proto__ = null;
obj.__proto__ = Object.prototype;
window.alert(Object.prototype.isPrototypeOf(obj)); //false
I use `obj.__proto__ = null;` to create short prototype chain and when
I want to do some things over own properties of that object. For
example, enumerate only own properties, without examine whole
prototype chain for properties which are not have {DontEnum}
attribute. And of course that save me of calling `hasOwnProperty` on
each iteration.
I use workaround with intermediate object in prototype chain.
var obj = {},
proto = obj.__proto__;
obj.__proto__ = {__proto__ : null};
// Do something with own properties
obj.__proto__ = proto; // Restore previous state of prototype chain
Is this behavior is a bug in SpiderMonkey? I do not found any
information about that. Perhaps the readers of c.l.js can explain why
they implement that in this way.
Thanks for responses.
internal [[Prototype]] property. More information can be found on:
<URL: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/proto
/>
In SpiderMonkey implementation every native object has own property
`__proto__`, which refer next object in prototype chain.
var obj = {};
window.alert(obj.__proto__ === Object.prototype); //true
The next object in prototype chain of object referred by `obj' is
Object.prototype. The prototype chain of that object looks:
obj -> Object.prototype -> null
window.alert(obj.hasOwnProperty('__proto__')); //true
window.alert(obj.propertyIsEnumerable('__proto__')); //false
`__proto__' is own property of that object and has internal attribute
{DontEnum}, which mean that property cannot be enumerate during `for-
in` loop.
window.alert(delete obj.__proto__); //false
Even {DontEnum} attribute that property has and {DontDelete} internal
attribute.
But if I assign primitive null value on that property in SpiderMonkey
some of the described things above are changed.
var obj = {__proto__ : null};
window.alert(Object.prototype.propertyIsEnumerable.call(obj,
'__proto__')); //false
window.alert(delete obj.__proto__); //true
`__proto__' still have {DontEnum} attribute but do not have
{DontDelete} yet. That is not the big problem. The problem is
`__proto__' yet is not mimic of internal [[Prototype]]. The previous
state of prototype chain cannot be restored.
var obj = {};
obj.__proto__ = null;
obj.__proto__ = Object.prototype;
window.alert(Object.prototype.isPrototypeOf(obj)); //false
I use `obj.__proto__ = null;` to create short prototype chain and when
I want to do some things over own properties of that object. For
example, enumerate only own properties, without examine whole
prototype chain for properties which are not have {DontEnum}
attribute. And of course that save me of calling `hasOwnProperty` on
each iteration.
I use workaround with intermediate object in prototype chain.
var obj = {},
proto = obj.__proto__;
obj.__proto__ = {__proto__ : null};
// Do something with own properties
obj.__proto__ = proto; // Restore previous state of prototype chain
Is this behavior is a bug in SpiderMonkey? I do not found any
information about that. Perhaps the readers of c.l.js can explain why
they implement that in this way.
Thanks for responses.