Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Joost Diepenmaat <joost@xxxxxxxxx>
- Date: Tue, 29 Jan 2008 00:45:42 +0100
Thomas 'PointedEars' Lahn <PointedEars@xxxxxx> writes:
function a() { alert("First") }
function b() { a() };
function a() { alert("Second") }
b();
The second declaration for `a' overwrites the reference with one to another
Function object before b() is executed. ISTM you have just proved that
there is a closure :)
I expected that reply and have prepared the following, demonstrating
that the lookup of a() in b() is done via dynamic, not lexical scoping.
function a() { alert("first") }
function b() { a() }
c = { a: function() { alert("second") } };
a();
with (c) {
a();
}
every function that calls another function,Not every function. It depends on where the other function was defined.
See code above. We're accessing a global property, not a lexical
variable. The fact that the variable is a function doesn't matter.
That seems contradictory.
Javascript variables that aren't declared (with var) in a function scope
or as a function argument are dynamic, defaulting to the global object's
property via whatever with() scopes are in place.
The same goes for functions: named functions defined in the global scope
are set as dynamic variables. Named functions defined within a with(X)
in the global scope are put in X. Named functions defined within a
function scope are lexical. I'm not sure what with() within a function
does.
Compare the above with:
function a() {
alert("first");
}
function b() { a() }
function c() {
function a() {
alert("second");
};
b();
}
a();
c();
or accesses any other global property would be a closure:That is correct. Probably that is why Flanagan made his oversimplifying
[...]
false statement.
Which statement?
That one cited in
<bc992427-bf06-4308-8416-b82396340578@xxxxxxxxxxxxxxxxxxxxxxxxxx>
Different thread, same source of confusion. Sigh. [psf 10.1]
I don't see the connection.
Joost.
.
- Follow-Ups:
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Joost Diepenmaat
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- References:
- "x.constructor == Foo" vs "x instanceof Foo"
- From: kj
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Joost Diepenmaat
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Thomas 'PointedEars' Lahn
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: RobG
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Thomas 'PointedEars' Lahn
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Joost Diepenmaat
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Thomas 'PointedEars' Lahn
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Joost Diepenmaat
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Thomas 'PointedEars' Lahn
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Joost Diepenmaat
- Re: "x.constructor == Foo" vs "x instanceof Foo"
- From: Thomas 'PointedEars' Lahn
- "x.constructor == Foo" vs "x instanceof Foo"
- Prev by Date: Re: "x.constructor == Foo" vs "x instanceof Foo"
- Next by Date: Re: "x.constructor == Foo" vs "x instanceof Foo"
- Previous by thread: Re: "x.constructor == Foo" vs "x instanceof Foo"
- Next by thread: Re: "x.constructor == Foo" vs "x instanceof Foo"
- Index(es):
Relevant Pages
|