By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

closure question

P: n/a
In the following code:
-----------------------
function get() {
return function() {
alert(x);
}
};
function foo(s) {
var x = s;
this.getX = get();
}
var f = new foo("hello");
f.getX()
--------------------------

Instead of printing "hello", f.getX() gives a JS error
of ("x is not defined").

Can someone explain Why in detail ?

Sep 24 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sep 24, 2:45*pm, JavascriptProgrammer <nowh...@nowhere.comwrote:
In the following code:
-----------------------
function get() {
return function() {
* * * * alert(x);
The x here is not declared, so when the function is called it will go
looking for it on its scope chain.
* * * * }};

function foo(s) {
* * * * var x = s;
x here is a local variable of the function foo.
* * * * this.getX = get();
* * * * }
var f = new foo("hello");
f doesn't have an x property, it was declared as a property of the
constructor. The function get() was called from inside the
constructor, but since it's declared outside the constructor, it
doesn't have a closure to x.

f.getX()
--------------------------

Instead of printing "hello", f.getX() gives a JS error
of ("x is not defined").
'cos it ain't. :-)

Move the function declaration for get inside the constructor, or use a
statement:
function foo(s) {

function get() {
return function() {
alert(x);
}
}

var x = s;
this.getX = get();
}
or, for simplicity's sake:

function foo(s) {
this.getX = function() {
alert(s);
}
}
Can someone explain Why in detail ?
There is no closure. Closures are created by how you declare a
function, not by how you call it. To create a closure, you declare a
function (or use a function expression) inside another function:

function foo() {
var x;
function bar() { // Create a closure
alert(x); // Use it to access foo's x
}
}
--
Rob
Sep 24 '08 #2

P: n/a
On 2008-09-24 06:45, JavascriptProgrammer wrote:
Instead of printing "hello", f.getX() gives a JS error
of ("x is not defined").

Can someone explain Why in detail ?
What you expected would indeed be correct in a dynamically scoped
language, but JavaScript uses lexical scope. Here's a good explanation
of the difference between the two:

http://en.wikipedia.org/wiki/Scope_(programming)
- Conrad
Sep 24 '08 #3

P: n/a
Conrad Lender <cr******@yahoo.comwrites:
On 2008-09-24 06:45, JavascriptProgrammer wrote:
>Instead of printing "hello", f.getX() gives a JS error
of ("x is not defined").

Can someone explain Why in detail ?

What you expected would indeed be correct in a dynamically scoped
language, but JavaScript uses lexical scope. Here's a good explanation
of the difference between the two:

http://en.wikipedia.org/wiki/Scope_(programming)
Correct. And to expand a bit, as far as I know closures are /defined/ to
work on lexicals. You can do similar things with dynamic variables (if
your language supports them) but many of the really interesting uses of
anonymous functions require lexical scope and closures:
function make_closure() {
var i = 0;
return function () { return ++i };
}

var c1 = make_closure();
c1(); // 1

var c2 = make_closure();
c2(); // 1

c1(); // 2
c1(); // etc...
c2();
c1();

See also:
http://en.wikipedia.org/wiki/Closure_(computer_science)

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Sep 24 '08 #4

P: n/a
RobG wrote:
JavascriptProgrammer wrote:
>function foo(s) {
var x = s;

x here is a local variable of the function foo.
> this.getX = get();
}
var f = new foo("hello");

f doesn't have an x property, it was declared as a property of the
constructor. [...]
Because `x' was declared a local variable of the constructor's *execution
context*, it became a property of its *Variable Object*. As became `s', BTW.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 26 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.