你不知道的Javascript - 霍雍


So, you think you know JavaScript? by Bosn Ma bosnma@live.com <> part 3 DIR 1). DOM levels 2). Project: Rigel Automation Platform 3). Execution Context 4). Named Function Expressions 5). etc 2012.03 => NOW 一淘数据平台FE 花名: 霍 雍 真名: 马圣博 微博: @Bosn 爱好: 写歌、健身、游泳 邮件: bosnma@live.com 2011.07 => 2012.02 独到科技Co-Founder 2009.12 => 2011.06 百度ECOM-FE Execution Context Activation Object Variable Object Global Context Window Object var a = 10; (function() { var b = 20; })(); alert(a); // 10 alert(b); // error, b in not defined for (var item in {a : 1, b : 2}) { alert(item); } alert(item); // item still in scope Global Context Function Context how and where the interpreter finds our data(functions, variable)? A variable object (in abbreviated form — VO) is a special object related with an execution context and which stores: 1. variables 2. function declarations 3. function formal parameters activeExecutionContext = { VO : { // VO => Variable Object data_var, // var i = 0; data_func_declaration, // function foo() {} data_func_arguments // func(30, 20) } }; var a = 10; function test(x) { var b = 20; } test(30); VO(globalContext) = { a : 10, test : }; VO(test functionContext) = { x : 30, b: 20 }; GlobalContextVO (VO === this === global === window) FunctionContextVO (VO === AO) VO(globalContext) === global; global = { Math : <...>, String : <...>, isNaN : function() {[Native Code]} ... ... window : global }; String(10); // means global.String(10); // with prefixes window.a = 10; // === global.window.a = 10 === global.a = 10; this.b = 20; // global.b = 20; VO(functionContext) === AO; AO = { arguments : }; arguments = { callee, // ref to the current function length, // quantity of real passed arguments properties-indexes // arguments[0] => param1, //arguments[1] => param2, ... ... }; => [].join.apply(arguments, ‘,’); function foo(x, y, z) { alert(foo.length); // 3 alert(arguments.length); // 2 alert(arguments.callee === foo); // true alert(x === arguments[0]); // true alert(x); // 10 arguments[0] = 20; alert(x); // 20 x = 30; alert(arguments[0]); // 30 z = 40; alert(arguments[2]); // undefined arguments[2] = 50; alert(z); // 40 } foo(10, 20); Phases of processing the context code 1. Entering the execution context AO(test) = { a: 10, b: undefined, c: undefined, d: e: undefined }; Function Expression doesn’t affect on VO VO filled with the following properties: 1. formal parameter (if not passed in => “*name+ : undefined” created) 2. FD (function, declaration, if conflicts => replace it) 3. var (“*name+:undefined” created, if conflicts => ignored!) function test(a, b) { var c = 10; function d() {} var e = function _e() {}; (function x() {}); b = 20; } test(10); l 2. Code Execution VO*‘c’+ = 10; VO*‘e’+ = function _e() {}; VO*‘b’+ = 20; function test(a, b) { var c = 10; function d() {} var e = function _e() {}; (function x() {}); b = 20; } test(10); l AO(test) = { a: 10, b: undefined, c: undefined, d: e: undefined }; AO(test) = { a: 10, b: 20, c: 10, d: e: function _e() {}; }; alert(x); var x = 10; alert(x); x = 20; function x() {} alert(x); if (true) { var a = 1; } else { var b = true; } alert(a); alert(b); // function // 10 // 20 // 1 // undefined a = 10; alert(window.a); alert(delete a); alert(window.a); var b = 20; alert(window.b); alert(delete b); alert(window.b); eval('var a = 10;'); alert(window.a); alert(delete a); alert(window.a); // 10 // true // undefined // 20 // false // still 20 Variables {DontDelete} attribute // 10 // true // undefined Firebug & Chrome console pitfall REFERENCE http://dmitry.baranovskiy.com/post/91403200 Thank U Let’s talk about NFE <>, part 4
还剩18页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享pdf获得金币 ] 0 人已下载

下载pdf

pdf贡献者

599316527

贡献于2012-08-05

下载需要 10 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf