Prev: ANUSHKA HOT PICTURES FOR BOLLYWOOD FANS
Next: FAQ Topic - What does the future hold for ECMAScript? (2010-07-20)
From: Ry Nohryb on 9 Aug 2010 09:34 On Aug 9, 2:11 pm, williamc <te...(a)williamc.com> wrote: > > Well, what is the correct way to think about scope in Javascript? I tend > to think of it as > > global > |_ function > |_ inner function > |_ inner, inner function > |_ etc... > > but that's probably naive. It's perfect, that's the way it is, but only if the graph is showing function declarations and not function calls (only if it's not a call chain): NOT: function f () { var k= 27; return inner() } function inner () { return innerinner() } function innerinner () { return k } f() --> ReferenceError: Can't find variable: k Call chain: f()-->inner()-->innerinner() YES: function f () { var k= 27; return inner(); function inner () { return innerinner(); function innerinner () { return k } } } f() --> 27 Call chain: f()-->inner()-->innerinner() -- Jorge.
From: williamc on 9 Aug 2010 10:03 On 8/9/2010 9:34 AM, Ry Nohryb wrote: > On Aug 9, 2:11 pm, williamc <te...(a)williamc.com> wrote: >> >> Well, what is the correct way to think about scope in Javascript? I tend >> to think of it as >> >> global >> |_ function >> |_ inner function >> |_ inner, inner function >> |_ etc... >> >> but that's probably naive. > > It's perfect, that's the way it is, but only if the graph is showing > function declarations and not function calls (only if it's not a call > chain): > > NOT: > function f () { var k= 27; return inner() } > function inner () { return innerinner() } > function innerinner () { return k } > > f() > --> ReferenceError: Can't find variable: k > Call chain: f()-->inner()-->innerinner() > > YES: > function f () { > var k= 27; > return inner(); > function inner () { > return innerinner(); > function innerinner () { return k } > } > } > > f() > --> 27 > Call chain: f()-->inner()-->innerinner() > -- > Jorge. thx, yeah that makes sense. I wonder if the following code from Mozilla Dev Center kind of illustrates the tendency to conflate scope with the this object? I know 'scope' in this function is only a local variable *name*, but it struck me as kind of a misleading in the 'way to think about it' department... if (!Array.prototype.forEach) { Array.prototype.forEach = function(fn, scope) { scope = scope || this; for(var i = 0; i < this.length; i++) { fn.call(scope, this[i], i, this); } }; } -- --williamc
From: williamc on 9 Aug 2010 10:13 On 8/9/2010 10:03 AM, williamc wrote: > On 8/9/2010 9:34 AM, Ry Nohryb wrote: >> On Aug 9, 2:11 pm, williamc <te...(a)williamc.com> wrote: >>> >>> Well, what is the correct way to think about scope in Javascript? I tend >>> to think of it as >>> >>> global >>> |_ function >>> |_ inner function >>> |_ inner, inner function >>> |_ etc... >>> >>> but that's probably naive. >> >> It's perfect, that's the way it is, but only if the graph is showing >> function declarations and not function calls (only if it's not a call >> chain): >> >> NOT: >> function f () { var k= 27; return inner() } >> function inner () { return innerinner() } >> function innerinner () { return k } >> >> f() >> --> ReferenceError: Can't find variable: k >> Call chain: f()-->inner()-->innerinner() >> >> YES: >> function f () { >> var k= 27; >> return inner(); >> function inner () { >> return innerinner(); >> function innerinner () { return k } >> } >> } >> >> f() >> --> 27 >> Call chain: f()-->inner()-->innerinner() >> -- >> Jorge. > > thx, yeah that makes sense. I wonder if the following code from Mozilla > Dev Center kind of illustrates the tendency to conflate scope with the > this object? > > I know 'scope' in this function is only a local variable *name*, but it > struck me as kind of a misleading in the 'way to think about it' > department... > > if (!Array.prototype.forEach) { > Array.prototype.forEach = function(fn, scope) { > scope = scope || this; > for(var i = 0; i < this.length; i++) { > fn.call(scope, this[i], i, this); > } > }; > } > Oops, had my head up my butt on that one. I meant to say 'argument name', not 'local variable name', and that function is *not* from Mozilla, I believe it's from one of the Sitepoint books. -- --williamc
From: Ry Nohryb on 9 Aug 2010 10:52 On Aug 9, 4:13 pm, williamc <te...(a)williamc.com> wrote: > On 8/9/2010 10:03 AM, williamc wrote: > > > > > > > > > > > On 8/9/2010 9:34 AM, Ry Nohryb wrote: > >> On Aug 9, 2:11 pm, williamc <te...(a)williamc.com> wrote: > > >>> Well, what is the correct way to think about scope in Javascript? I tend > >>> to think of it as > > >>> global > >>> |_ function > >>> |_ inner function > >>> |_ inner, inner function > >>> |_ etc... > > >>> but that's probably naive. > > >> It's perfect, that's the way it is, but only if the graph is showing > >> function declarations and not function calls (only if it's not a call > >> chain): > > >> NOT: > >> function f () { var k= 27; return inner() } > >> function inner () { return innerinner() } > >> function innerinner () { return k } > > >> f() > >> --> ReferenceError: Can't find variable: k > >> Call chain: f()-->inner()-->innerinner() > > >> YES: > >> function f () { > >> var k= 27; > >> return inner(); > >> function inner () { > >> return innerinner(); > >> function innerinner () { return k } > >> } > >> } > > >> f() > >> --> 27 > >> Call chain: f()-->inner()-->innerinner() > >> -- > >> Jorge. > > > thx, yeah that makes sense. I wonder if the following code from Mozilla > > Dev Center kind of illustrates the tendency to conflate scope with the > > this object? > > > I know 'scope' in this function is only a local variable *name*, but it > > struck me as kind of a misleading in the 'way to think about it' > > department... > > > if (!Array.prototype.forEach) { > > Array.prototype.forEach = function(fn, scope) { > > scope = scope || this; > > for(var i = 0; i < this.length; i++) { > > fn.call(scope, this[i], i, this); > > } > > }; > > } > > Oops, had my head up my butt on that one. I meant to say 'argument > name', not 'local variable name', and that function is *not* from > Mozilla, I believe it's from one of the Sitepoint books. Yep, it's very often that I see too 'this' called "the scope", nowadays. Maybe it's so called in some other language... (?) -- Jorge.
From: John G Harris on 9 Aug 2010 10:55
On Sun, 8 Aug 2010 at 12:45:43, in comp.lang.javascript, David Mark wrote: <snip> >You seem to have missed the point that it is not a matter of knowledge >but communication. The specs are written for *implementors*, not >programmers. That's why JS programmers don't refer to the language as >"ECMAScript" and rarely talk of syntax in terms of "productions". ;) So you feel that it's wrong to say anything in this news group that would not be understood by the people who write JQuery ? An amazing number of programmers don't know what a statement is, including some with PhDs. That's no reason for not using the word as defined in the language specification. <snip> >There is no need for a term for such a general case (except perhaps >for implementors). Trying to use such a term in programming >discussions will only serve to confuse. C++ manages to find a use for such a general case, but then it's a language for real programmers. Your definition can be thoroughly confusing at times : 1. Back in the days of ECMA 262 v2 you could have a program where undefined was a variable in one browser and not a variable in another (because it was pre-defined). This is confusing. 2. Even now in v3 you can write to undefined, so it now suddenly becomes a variable (because it has now been implicitly declared). This is confusing. 3. When someone uses My Library they access a thing called API. It's not a variable according to you (because they didn't declare it). This is also confusing. John -- John Harris |