can I chain an object into a global namespace ?

C

chichkov

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.")
 
R

RobG

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).
 
C

chichkov

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) {...}...)
 
T

Touffy

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.
 
T

Touffy

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.
 
M

Michael Winter

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
 
R

Richard Cornford

Touffy said:
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.
 
R

Richard Cornford

RobG wrote:
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.
 
T

Touffy

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.
 
V

VK

Touffy said:
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").
 
M

Michael Winter

Touffy said:
On 2006-10-06 17:03:28 +0200, Michael Winter
<[email protected]> 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
 
C

chichkov

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.
 
R

Richard Cornford

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.
 

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,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top