EcmaScript 5 Google TechTalk - Random Example 1 Example

I watch this lecture: http://www.youtube.com/watch?v=Kq4FpMe6cRs

Ca1Aj.png

// the speaker states that "'bar' is just some function 
// that invokes whatever function is passed to it"
function bar(fn) {
    fn();
}

function foo() {
    var x = 8;
    bar(function baz() { return x; });
}

Object.prototype.x = 'foo';

At minute 35, the above number is presented. The lecturer claims that some browsers will return fooinstead 8.

Why?

By the way, when I wrote this question, I understood, but I will post this question anyway, because this is an interesting problem. :)


Live demo: http://jsfiddle.net/simevidas/mHyKc/

Warnings Opera 11 'foo', all my other browsers (including IE9) return 8.


: , , . , . (baz), Opera 8, , .

?

+5
1

, . , , , . http://kangax.github.com/nfe/#spidermonkey-peculiarity

, . , " ". :

var fn = function aNamedFunction() {
  typeof aNamedFunction;  // "function"
};
typeof aNamedFunction;  // "undefined"

, SpiderMonkey JS- (.. , ) , . , new Object() aNamedFunction .

x , . var x;, , , . new Object() ( - ), x , JavaScript. , Object.prototype, 'foo' x .

ES5.

: http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#nfe-and-spidermonkey

+4

All Articles