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 ?

 
 
chichkov@gmail.com
Guest
Posts: n/a
 
      10-06-2006
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

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

 
Reply With Quote
 
 
 
 
chichkov@gmail.com
Guest
Posts: n/a
 
      10-06-2006
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


 
Reply With Quote
 
Touffy
Guest
Posts: n/a
 
      10-06-2006
On 2006-10-06 08:29:49 +0200, (E-Mail Removed) 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

 
Reply With Quote
 
Touffy
Guest
Posts: n/a
 
      10-06-2006
On 2006-10-06 16:11:55 +0200, Touffy <(E-Mail Removed)> 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

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-06-2006
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
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      10-06-2006
Touffy wrote:
> On 2006-10-06 16:11:55 +0200, Touffy <(E-Mail Removed)> 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.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      10-06-2006
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.

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

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      10-06-2006
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").

 
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