最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript function scope - Stack Overflow

programmeradmin5浏览0评论

Who can explain why result are [20, 20, 10, 10] in this code:

var x = 10;
var foo = {
  x: 20,
  bar: function () {
    var x = 30;
    return this.x;
  }
};

console.log(
  foo.bar(),
  (foo.bar)(),
  (foo.bar = foo.bar)(),
  (foo.bar, foo.bar)()
);

Links to specification are weled

Who can explain why result are [20, 20, 10, 10] in this code:

var x = 10;
var foo = {
  x: 20,
  bar: function () {
    var x = 30;
    return this.x;
  }
};

console.log(
  foo.bar(),
  (foo.bar)(),
  (foo.bar = foo.bar)(),
  (foo.bar, foo.bar)()
);

Links to specification are weled

Share Improve this question asked Feb 19, 2010 at 9:45 anton_byrnaanton_byrna 2,5751 gold badge18 silver badges31 bronze badges 1
  • in IE it seems the output is 20,20,undefined,undefined.. – RameshVel Commented Feb 19, 2010 at 9:57
Add a ment  | 

3 Answers 3

Reset to default 7

Can't point you at specification, but i can highly remend reading Douglas Crockford's "Javascript: The good parts". This book will help you understand most of the weird but great features of JavaScript.

As of your question:

  1. foo.bar(), this keyword in bar function is bound to foo object
  2. (foo.bar)() is the same as above,
  3. In javascript you can assign variables from right to left multiple times

    z = 3; x = (y = z); console.log(x); //3

functions are variables as anything else. So you are assigning the function foo.bar to foo.bar, but the parenthesis causes the assigned function to be returned, and then executed.

(foo.bar = foo.bar)(); 
//is the same as
var f = (foo.bar = foo.bar);
f(); 
//and this also the same as:
var f= foo.bar;
f();

The function returned from parenthesis is not bound to anything, so this will refer to global object, in case of browsers - to the window object.

4.. The clause (foo.bar, foo.bar)() is just alike:

a = (3, 4); //last value is returned, first just parsed.
//a contains 4

var f = (foo.bar, foo.bar); 
//f contains body of foo.bar function, 
f() // is executed  in the context of `global` object, eg. `window`. 

Please read about binding of functions in JavaScript.

I think following question will be helpful for this.

The first two function calls are equivalent. They are calling foo's bar method within the context of foo – therefore the value returned by this.x is the value of foo's x property, which is 20.

The second two calls are both questionable/invalid syntax. Try running your code through JSLint and you'll that it spits out several errors and then chokes entirely. My best guess as to why they return 10 is that it's attempting to parse your code in a case where it really shouldn't and is getting confused. 10 might be returned because the browser cannot figure out what you're trying to do and is defaulting to global (window) scope where the value of x is 10. This would also explain Ramesh Vel's ment that the second two values appear as undefined in IE. Due to the fact that the syntax is invalid, different implementations of JavaScript are likely to handle them differently.

发布评论

评论列表(0)

  1. 暂无评论