Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > can I chain an object into a global namespace ?

Reply
Thread Tools

can I chain an object into a global namespace ?

 
 
Michael Winter
Guest
Posts: n/a
 
      10-06-2006
Touffy wrote:

> On 2006-10-06 17:03:28 +0200, Michael Winter
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
chichkov@gmail.com
Guest
Posts: n/a
 
      10-06-2006
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.


 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      10-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional import into global namespace mk Python 0 03-02-2010 05:46 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
How to import whole namespace into global symbol table? (newbie) Scott Simpson Python 2 04-28-2006 05:51 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM



Advertisments