Adding properties to Object to affect all host objects

Discussion in 'Javascript' started by Yan Huang, Oct 13, 2009.

  1. Yan Huang

    Yan Huang Guest

    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
     
    Yan Huang, Oct 13, 2009
    #1
    1. Advertising

  2. Yan Huang

    RobG Guest

    On Oct 14, 6:59 am, Yan Huang <> wrote:
    > 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).


    --
    Rob
     
    RobG, Oct 15, 2009
    #2
    1. Advertising

  3. Yan Huang

    Yan Huang Guest

    On Oct 14, 10:07 pm, RobG <> wrote:
    > On Oct 14, 6:59 am, Yan Huang <> wrote:
    >
    > > String.__myOwnProp__;   // 1
    > > window.__myOwnProp__;   // 1
    > > window.document.__myOwnProp__;   // 1

    >
    > In some browsers, yes. Try it in IE.


    I was actually working on a project specially targeted at Opera.

    Thanks a lot for your reply, though.

    -- Yan Huang
    >
    > > 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).
    >
    > --
    > Rob
     
    Yan Huang, Oct 15, 2009
    #3
  4. Yan Huang wrote:

    > 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
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Oct 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Some One
    Replies:
    5
    Views:
    1,023
    Toby A Inkster
    Aug 9, 2003
  2. Victor Porton
    Replies:
    1
    Views:
    721
    Steven T. Hatton
    Aug 29, 2004
  3. Sonal
    Replies:
    3
    Views:
    694
  4. Filip Gruszczyński

    Do more imported objects affect performance

    Filip Gruszczyński, Dec 1, 2008, in forum: Python
    Replies:
    6
    Views:
    316
    Nick Craig-Wood
    Dec 3, 2008
  5. ytrewq
    Replies:
    3
    Views:
    222
    ytrewq
    Jan 8, 2005
Loading...

Share This Page