extend prototype chain?

D

Dmitry A. Soshnikov

`window' refers to a host object, _not_
the built-in Global Object.

Everybody knows (really).

Regarding to browser scripting and its host environment there're some
specific cases in specific implementations when `this' and `window'
will return different results for manipulating with some data (for
example, `hasOwnProperty' and variable declarations in current version
of Opera).

But regarding to property accessor - do you know some browser host
environment (regardless its implementation of mapping properties for
`this' and `window') where reading from `this' (global) and `window'
for the same property, will provide different results?

So regardless the specific cases (and especially for those when
property has been set to `window' object), accessing properties of
`window' is acceptable. Moreover, some implementations have some
optimization for reading `window' name in identifier resolution.
On a personal note, Jorge has been told these and other basic things a
hundred times before already, but he would not listen (or try to
understand).  The record shows he is likely only trolling; ignore him.

Try to speak to the point only. This sentence will be completely
correct in its meaning if you do .replace(/Jorge/g, "Thomas
'PointedEars' Lahn") ;)

/ds
 
J

Jorge

I was wondering about that passage in ECMA-262, Section 10.1.5:
"[...] for example, in the HTML document object model the window property
of the global object is the global object itself."
That matches what you're saying and contradicts what several others are
saying. The question is, is this normative to the DOM or descriptive
(e.g. for the time it was written)?

As Richard said.  I would like to add an explanation as to why it is
"descriptive (and only an example)", hopefully for future reference:

FOR FUTURE REFERENCE:

The browser provides not one but *two* aliases of the global object:

window === globalObject; and self === globalObject;

Both are themselves properties of the globalObject:

window === globalObject.window; and self === globalObject.self;
"window" in this === "self" in this === true;

And both are as you can easily see, circular references:

globalObject.window= globalObject; and globalObject.self=
globalObject;
^ | ^ |
|_______________________| |_________________|

as in a= {}; a.self= a;

Any property of the window object is a property of the globalObject
(because they are exactly the same object), e.g. window.top ===
this.top === globalObject.top, and "top" in this === true, or "top" in
(function(){return this;})() === true, etc.

It's neither so difficult to comprehend, nor so difficult to remember.
 
E

Eric Bednarz

Jorge said:
And, what's wrong with global.window= global; ?

I would put my bet on a runtime error in IE < 8 and breaking
(window.)onload in IE 8, so nothing you would care about.
 
J

Jorge

I would put my bet on a runtime error in IE < 8 and breaking
(window.)onload in IE 8, so nothing you would care about.

That wasn't code intended to be run. It was meant to explain what
window is: a property of the globalObject that points to itself.
 
D

Dmitry A. Soshnikov

[...]
It was meant to explain what
window is: a property of the globalObject that points to itself.

Except specific cases in specific implementations. Such as in IE8:

alert([
this === window), // true
this.window === window, // false
this.window == window, // true
this.window === window.window // true
]);

Or what I mentioned, variable declaration and `hasOwnProperty' in
Opera 10.10 WinXP (that `var's are direct properties of
`this' (global) and not `window's - if can trust to `hasOwnProperty'):

var z = new Number(10);
alert([
this === window, // true
'z' in this, // true
'z' in window, // true
window.z === this.z, // true
Object.prototype.hasOwnProperty.call(this, 'z'), // true
Object.prototype.hasOwnProperty.call(window, 'z') // false (!)
]);

Or back to IE8, the example which I showed not once already about
shadowing host properties of `window':

alert(navigator); // [object], OK
alert(navigator.userAgent); // exactly that object we expect

// now use variable declaration
// using [eval] which should not
// set {DontDelete}

eval('var navigator;');
alert(navigator); // undefined, OK

// set new value
navigator = 10;
alert(navigator); // 10, OK

// remove it from the base object
// as navigator has no {DontDelete}
delete navigator;

alert(navigator); // [object], ;)

// and exactly restored main
// [navigator] host object
alert(navigator.userAgent);

Here's my (guessing) explanation: <URL:
http://groups.google.ru/group/comp.lang.javascript/msg/c1572441853282c6?hl=en>

But regardless this specific cases, reading properties of `window' or
`this' (global) - I didn't see yet implementation where it cause an
errors or gives different results (if you know, let me know).

/ds
 
T

Thomas 'PointedEars' Lahn

Eric said:
Jorge said:
And, what's wrong with global.window= global; ?

I would put my bet on a runtime error in IE < 8 [...]

You would win. The second line of

var global = this;
global.window = global;

throws a ReferenceError(!) with description "Illegal assignment" in
IE 6.0.2800.1106.


PointedEars
 
J

Jorge

(...)
  var global = this;
  global.window = global;

throws a ReferenceError(!) with description "Illegal assignment" in
IE 6.0.2800.1106.

What part of "not intended to be run" you don't understand ?
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top