can I chain an object into a global namespace ?

Discussion in 'Javascript' started by chichkov@gmail.com, Oct 6, 2006.

  1. Guest

    Hi,

    Can I chain an object into a global namespace ?
    Like "with(object) {....}", but I'd like to see it at the _end_ of
    the chain, not at the top...


    following code works, but not neat either...
    ------------------------
    var sk = new Object
    sk.aaa = 1

    for (var item in sk) this[item] = sk[item];

    if(aaa == 1) alert("Success.")
    else alert("Failure.")
    ------------------------

    -- Regards, Mitra
    , Oct 6, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > Hi,
    >
    > Can I chain an object into a global namespace ?
    > Like "with(object) {....}", but I'd like to see it at the _end_ of
    > the chain, not at the top...


    The terms start and end are somewhat ambiguous here.


    > following code works, but not neat either...
    > ------------------------
    > var sk = new Object
    > sk.aaa = 1
    >
    > for (var item in sk) this[item] = sk[item];


    That just adds every enumerable property of sk to the global object
    (presuming it is run in a global context).

    >
    > if(aaa == 1) alert("Success.")
    > else alert("Failure.")
    > ------------------------


    I think what you are saying is that you want to be able to resolve
    identifiers against sk after the global object, e.g. rather than:

    global <-- sk

    you want

    sk <-- global

    and you can do do:

    alert(aaa);

    in any context and that once identifier resolution has searched the
    scope chain and reached the global object, it then tries sk and alerts
    '1', correct?

    The simple answer is no - you can't displace the global object as the
    top of the scope chain (at least I don't think you can).


    --
    Rob
    RobG, Oct 6, 2006
    #2
    1. Advertising

  3. Guest

    Yes. I want to setup following chain 'global -> sk'.
    So if I will try alert(aaa) it should search global scope first and
    then try 'sk'.

    It should be something like this.parent = sk (and I don't mind if it
    will work only in IE...)

    -- Regards, Mitra


    >
    > in any context and that once identifier resolution has searched the
    > scope chain and reached the global object, it then tries sk and alerts
    > '1', correct?


    Exactly. And I don't like it searching in the 'sk' first (like
    with......with(sk) {...}...)

    >
    > --
    > Rob
    , Oct 6, 2006
    #3
  4. Touffy Guest

    On 2006-10-06 08:29:49 +0200, said:

    > Yes. I want to setup following chain 'global -> sk'.
    > So if I will try alert(aaa) it should search global scope first and
    > then try 'sk'.
    >
    > It should be something like this.parent = sk (and I don't mind if it
    > will work only in IE...)


    I tried:

    window.constructor.prototype=sk

    but it appears window.prototype is read-only.
    However, setting individual properties of the prototype works.

    window.constructor.prototype.aaa=1
    alert(aaa) // 1
    window.aaa=3
    alert(aaa) // 3
    delete aaa
    alert(aaa) // 1

    Of course, window.constructor is a constructor for much more than just
    the window (depends on browser, eg in Safari it's the Object itself) so
    that's adding aaa in a lot more scopes than you may like.

    --
    David Junger
    Touffy, Oct 6, 2006
    #4
  5. Touffy Guest

    On 2006-10-06 16:11:55 +0200, Touffy <> said:

    > but it appears window.prototype is read-only.


    I meant window.constructor.prototype, which would be Object.prototype
    in the browser I used (Safari) so I suppose it's only natural that it's
    read-only since Object.prototype is Object itself so that would mean
    replacing the Object object.


    --
    David Junger
    Touffy, Oct 6, 2006
    #5
  6. Touffy wrote:

    [snip]

    > I tried:
    >
    > window.constructor.prototype=sk
    >
    > but it appears window.prototype is read-only.


    The prototype chain of the global object is implementation-defined.
    There doesn't need to be one at all ([[Prototype]] may be null), and the
    global object doesn't need to have a constructor property, either.

    [snip]

    Mike
    Michael Winter, Oct 6, 2006
    #6
  7. Touffy wrote:
    > On 2006-10-06 16:11:55 +0200, Touffy <> said:
    >
    > > but it appears window.prototype is read-only.

    >
    > I meant window.constructor.prototype, which would be Object.prototype
    > in the browser I used (Safari) so I suppose it's only natural that it's
    > read-only since Object.prototype is Object itself so that would mean
    > replacing the Object object.


    Object is itself a function so - Object.prototype - cannot be Object or
    all objects would inherit - call - and - apply - methods.

    Richard.
    Richard Cornford, Oct 6, 2006
    #7
  8. RobG wrote:
    <snip>
    > I think what you are saying is that you want to be able to resolve
    > identifiers against sk after the global object, e.g. rather than:
    >
    > global <-- sk
    >
    > you want
    >
    > sk <-- global
    >
    > and you can do do:
    >
    > alert(aaa);
    >
    > in any context and that once identifier resolution has searched the
    > scope chain and reached the global object, it then tries sk and alerts
    > '1', correct?
    >
    > The simple answer is no - you can't displace the global object as the
    > top of the scope chain (at least I don't think you can).


    While it is not possible to move the global object from the end of the
    scope chain it would be possible to add the global object to the scope
    chain after the object in question. So any Identifier resolution
    visited the global object first, and in the event that it did get to
    the end of the scope chain it would not find anything on the global
    object there as if the global object had the property in question then
    it would already have been found on the global object higher up the
    scope chain.

    var sk = {aaa:'aaa on object', bbbb:'aaa on object'};
    var aaa = 'aaa on global';
    var global = this;
    with(sk){
    with(global){
    global.someFunction = function(){
    alert('aaa = '+aaa+'\nbbb = '+bbb);
    };
    }
    }
    someFunction();

    However, I suspect that a desire to do this indicates a failure to
    appreciate an more direct and simpler method of achieving the same
    required outcome.

    Richard.
    Richard Cornford, Oct 6, 2006
    #8
  9. Touffy Guest

    On 2006-10-06 17:03:28 +0200, Michael Winter <> said:

    > Touffy wrote:
    >
    > [snip]
    >
    >> I tried:
    >>
    >> window.constructor.prototype=sk
    >>
    >> but it appears window.prototype is read-only.

    >
    > The prototype chain of the global object is implementation-defined.


    window is the global object in client-side JavaScript, but Object is
    the prototype root object. Every object in JavaScript inherits from
    Object, including window.

    > There doesn't need to be one at all ([[Prototype]] may be null), and
    > the global object doesn't need to have a constructor property, either.


    window has a constructor property, as any browser can tell you. It is
    not necessarily Object itself, but window is definitely an instance of
    Object.

    The method I suggested is not very clean because it adds properties to
    Object.prototype (maybe another prototype in other browsers) and not
    just to a specific scope, but it does work on most browsers even when
    window.constructor is not Object.


    --
    David Junger
    Touffy, Oct 6, 2006
    #9
  10. VK Guest

    Touffy wrote:
    > window is the global object in client-side JavaScript, but Object is
    > the prototype root object.


    As it was pointed out already: what implementation are you talking
    about? It has nothing to do with at least one implementation (Microsoft
    JScript) but as this one covers ~80% or more of cases then it is way
    too wide-spread to disregard in generalizations.

    > Every object in JavaScript inherits from
    > Object, including window


    Which means then that frames[], document, body, div are in inheritance
    relations with Array, myArray and Math (thinking consecutively). Such
    attempt (to crossbreed Global and DOM) was made once on Gecko before
    XBL and I see the leftovers as attractive as a dead-born platypus :)
    :-|
    Global encompass the execution context of the current script, DOM
    provides interfaces for Global so you could communicate with the
    graphics context. Note: it provides *interfaces to*, not DOM elements
    themselves. That can be a huge difference.

    If you want a real generalization (lesser 1% of unknown to me but
    possible cases) then in the situations like
    <script type="text/javascript">
    var foo = 'bar';
    window.alert(foo); // 1]
    window.alert(window['foo']; // 2]
    </script>

    statement 1] and statement 2] are not equal and they act by very
    different schema - despite that the "visual" difference is as difficult
    to catch as two stars closer than 1' of distance.


    To OP: sorry, but within the traditional schema "one Document = one
    Global" nothing seems possible to be done (yet there are no limits to
    human's creativity, so I better leave "seems").
    VK, Oct 6, 2006
    #10
  11. Touffy wrote:

    > On 2006-10-06 17:03:28 +0200, Michael Winter
    > <> said:


    [snip]

    >> The prototype chain of the global object is implementation-defined.

    >
    > window is the global object in client-side JavaScript,


    The identifier, window, is a property of the global object that refers
    to that same object.

    > but Object is the prototype root object.


    Yes, so all built-in prototype objects, with the exception of the Object
    prototype object, will include the Object prototype object in their
    prototype chains.

    Every built-in prototype object has the Object prototype
    object, which is the initial value of the expression
    Object.prototype (15.3.2.1), as the value of its internal
    [[Prototype]] property, except the Object prototype object
    itself.
    -- Paragraph 8, Section 15 "Native ECMAScript Objects",
    ECMA-262 3rd Ed.

    That implies that all native objects that have a prototype chain will
    therefore also include the Object prototype object in their prototype
    chains. However, the global object doesn't necessarily have a prototype
    chain: that is left the implementation.

    The values of the [[Prototype]] and [[Class]] properties of the
    global object are implementation-dependent.
    -- Paragraph 3, Section 15.1 "The Global Object",
    ECMA-262 3rd Ed.

    > Every object in JavaScript inherits from Object, including window.


    Not true. Only the built-in prototype objects must include the Object
    prototype object in their prototype chains. Host objects and the global
    object may only if they wish.

    >> There doesn't need to be one at all ([[Prototype]] may be null),
    >> and the global object doesn't need to have a constructor property,
    >> either.

    >
    > window has a constructor property, as any browser can tell you.


    Any browser? MSIE doesn't. Even if it did, that doesn't mean that all
    browsers would. As I wrote previously, this an implementation-dependent
    matter. If an implementation, such as JavaScript, cares to make the
    simplification of using the Object prototype object in every prototype
    chain, that's its business, but it's far from a universal necessity.

    > It is not necessarily Object itself, but window is definitely an
    > instance of Object.


    The global object may be of [[Class]] Object, if an implementation so
    wishes, but that is all. Any object that doesn't have a [[Call]]
    property is defined this way unless explicitly overridden, so it would
    be sensible to follow suit.

    [snip]

    Mike
    Michael Winter, Oct 6, 2006
    #11
  12. Guest

    Hi,

    > However, I suspect that a desire to do this indicates a failure to
    > appreciate an more direct and simpler method of achieving the same
    > required outcome.
    >
    > Richard.



    The point was to keep compatibility with scripts that had that 'sk'
    object in the global namespace. Thanks for your answer!

    -- Regards, Mitra.



    > >
    > > in any context and that once identifier resolution has searched the
    > > scope chain and reached the global object, it then tries sk and alerts
    > > '1', correct?
    > >
    > > The simple answer is no - you can't displace the global object as the
    > > top of the scope chain (at least I don't think you can).

    >
    > While it is not possible to move the global object from the end of the
    > scope chain it would be possible to add the global object to the scope
    > chain after the object in question. So any Identifier resolution
    > visited the global object first, and in the event that it did get to
    > the end of the scope chain it would not find anything on the global
    > object there as if the global object had the property in question then
    > it would already have been found on the global object higher up the
    > scope chain.
    >
    > var sk = {aaa:'aaa on object', bbbb:'aaa on object'};
    > var aaa = 'aaa on global';
    > var global = this;
    > with(sk){
    > with(global){
    > global.someFunction = function(){
    > alert('aaa = '+aaa+'\nbbb = '+bbb);
    > };
    > }
    > }
    > someFunction();
    >
    > However, I suspect that a desire to do this indicates a failure to
    > appreciate an more direct and simpler method of achieving the same
    > required outcome.
    >
    > Richard.
    , Oct 7, 2006
    #12
  13. wrote:
    >> However, I suspect that a desire to do this indicates a
    >> failure to appreciate an more direct and simpler method
    >> of achieving the same required outcome.

    >
    >
    > The point was to keep compatibility with scripts that had
    > that 'sk' object in the global namespace. Thanks for your
    > answer!


    I don't see how that would be significant. If the - sk - object is "in
    the global namespace" then I would assume that a property of the global
    object refers to the object. In that case the properties of the - sk -
    object would not come into play during Identifier resolution.

    Richard.
    Richard Cornford, Oct 7, 2006
    #13
    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. Èý¹â
    Replies:
    1
    Views:
    555
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Jason Heyes
    Replies:
    1
    Views:
    440
    Woebegone
    Nov 19, 2004
  3. Scott Simpson
    Replies:
    2
    Views:
    366
    Peter Otten
    Apr 28, 2006
  4. Peng Yu
    Replies:
    0
    Views:
    626
    Peng Yu
    Sep 14, 2008
  5. mk
    Replies:
    0
    Views:
    347
Loading...

Share This Page