读书人

两个个关于javascript的scope的有趣的

发布时间: 2013-03-06 16:20:31 作者: rapoo

两个个关于javascript的scope的有趣的例子

// Scope insanity// a self-executing anonymous function(function() {  var baz = 1;  var bim = function() {    alert( baz );  };   bar = function() {    alert( baz );  }; })(); // baz is not defined outside of the functionconsole.log( baz ); // bar is defined outside of the anonymous function// because it wasn't declared with var; furthermore,// because it was defined in the same scope as baz,// it has access to baz even though other code// outside of the function does notbar(); // bim is not defined outside of the anonymous function,// so this will result in an errorbim();

?

?

来自jquery的教学课程:?http://learn.jquery.com/javascript-101/scope/

读书人网 >JavaScript

热点推荐