Adding properties to Object to affect all host objects

Y

Yan Huang

Hi,

I attempted to give all objects in a browser environment some special
property like __myOwnProp__, through the following code,

Object.prototype.__myOwnProp__ = 1;

This seems works fine with all native and user-defined objects,

String.__myOwnProp__; // 1
window.__myOwnProp__; // 1
window.document.__myOwnProp__; // 1

but not with dynamically created host objects supplied by the browser.
For example,

var w = window.open();
w.__myOwnProp__; // undefined

Anyone knows how to make this __myOwnProp__ property inheritable by a
dynamically created host objects like the w in the code above?

Thanks,
Yan Huang
 
R

RobG

Hi,

I attempted to give all objects in a browser environment some special
property like __myOwnProp__, through the following code,

Object.prototype.__myOwnProp__ = 1;

This seems works fine with all native and user-defined objects,

String.__myOwnProp__;   // 1
window.__myOwnProp__;   // 1
window.document.__myOwnProp__;   // 1

In some browsers, yes. Try it in IE.

but not with dynamically created host objects supplied by the browser.
For example,

var w = window.open();
w.__myOwnProp__;    // undefined

Anyone knows how to make this __myOwnProp__ property inheritable by a
dynamically created host objects like the w in the code above?

Most (but not all) native objects inherit from Object or Function.
Host objects are not required to inherit from any native object, or to
implement any inheritance pattern at all. While some browsers do
implement limited inheritance, some don't have any at all so you can't
do what you want in a cross-browser way.

This approach has been tried in libraries such as Prototype.js, it
falls back to extending host objects where it doesn't think there's an
inheritance mechanism. That approach is flawed, not least because
there is no guarantee that host objects can be extended either (i.e.
they may not allow non-standard properties to be added).
 
T

Thomas 'PointedEars' Lahn

Yan said:
I attempted to give all objects in a browser environment some special
property like __myOwnProp__, through the following code,

Object.prototype.__myOwnProp__ = 1;

You don't want to do that; this property would be enumerable.
This seems works fine with all native and user-defined objects,
[...]
Anyone knows how to make this __myOwnProp__ property inheritable by a
dynamically created host objects like the w in the code above?

w.__proto__.__myProp__ = 42;

but it would probably only work in Gecko-based browsers. You are much
better off using a wrapper object.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top