scope chain

A

Armel Asselin

In JavaScript / ECMAScript there is notion of scope chain... and separated
'this' object however in chapter 10.1.4 of ECMA 262 specification they say
'get next object in scope chain'... but when should I search for a member in
the 'this' object ?
is it put into the scope chain ? in that case, is it searched by
sub-functions that inherit the scope chain ??

Armel
 
L

Lasse Reichstein Nielsen

Armel Asselin said:
In JavaScript / ECMAScript there is notion of scope chain... and separated
'this' object however in chapter 10.1.4 of ECMA 262 specification they say
'get next object in scope chain'... but when should I search for a member in
the 'this' object ?

Only when you ask for it. You have to write "this.foo" to look for
"foo" in the current this-object [1]
is it put into the scope chain ?

No.

The scope chain is static. You can look at the program and see it.

The outermost scope is the global scope.
If you have a function, it generates a new scope. Code inside this
function can see the variables and functions declared inside the
function.
If you have another function inside the first, it too will generate
a scope, and the first function's scope will be the next in the
scope chain.

You can use the "with" statement to add an object to the scope chain,
but it can be dangerous to use, if you don't know *exactly* which
properties that object has.



/L
[1] Obviously, unless the this-object is in the scope chain for some
other reason, like it being the global object.
 
A

Armel Asselin

[1] Obviously, unless the this-object is in the scope chain for some
other reason, like it being the global object.

is it possible that for a widget event they made a scope chain like (global
object; form object; widget object; activation object) ?

it seems that simply writing something like :

<input type=button onclick="alert(value);" value="bonjour" />

it perfectly works in IE but it should not ! I should have written
"this.value" ?! so they must have every 'interesting' object in the scope
chain no ?

Armel
 
L

Lasse Reichstein Nielsen

Armel Asselin said:
[1] Obviously, unless the this-object is in the scope chain for some
other reason, like it being the global object.

is it possible that for a widget event they made a scope chain like (global
object; form object; widget object; activation object) ?

Yes. The chain seems to be:
global, document object, form object, widget/element object, activation
(without the form, if it is not inside a form). This also goes for, e.g.,
an image with onmouseover.
it seems that simply writing something like :
<input type=button onclick="alert(value);" value="bonjour" />
it perfectly works in IE but it should not !

There is no standard governing the evaluation of the HTML event
handler attributes, so "should" is a strong word. The original Netscapes
had this behavior, so it was copied by later browsers.
I should have written "this.value" ?! so they must have every
'interesting' object in the scope chain no ?

Close :)

/L
 

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

Forum statistics

Threads
473,798
Messages
2,569,649
Members
45,382
Latest member
tallzebra

Latest Threads

Top