第5版真实在
JavaScript functions are a combination of code to be executed and the scope in which to execute them. This combination of code and scope is known as a closure in the computer science literature. All JavaScript functions are closures. These closures are only interesting, however, in the case discussed above: when a nested function is exported outside the scope in which it is defined. When a nested function is used in this way, it is often explicitly called a closure.
When the JavaScript interpreter invokes a function, it first sets the scope to the scope chain that was in effect when the function was defined. Next, it adds a new object, known as the call object (the ECMAScript specification uses the term activation object) to the front of the scope chain. The call object is initialized with a property named arguments that refers to the Arguments object for the function. Named parameters of the function are added to the call object next. Any local variables declared with the var statement are also defined within this object. Since this call object is at the head of the scope chain, local variables, function parameters, and the Arguments object are all in scope within the function. This also means that they hide any properties with the same name that are further up the scope chain, of course.
When no nested functions are involved, the scope chain is the only reference to the call object. When the object is removed from the chain, there are no more references to it, and it ends up being garbage collected.
But nested functions change the picture. If a nested function is created, the definition of that function refers to the call objects because that call object is the top of the scope chain in which the function was defined. If the nested function is used only within the outer function, however, the only reference to the nested function is in the call object. When the outer function returns, the nested function refers to the call object, and the call object refers to nested function, but there are no other references to either one, and so both objects become available for garbage collection.
Things are different if you save a reference to the nested function in the global scope. You do so by using the nested function as the return value of the outer function or by storing the nested function as the property of some other object. In this case, there is an external reference to the nested function, and the nested function retains its reference to the call object of the outer function. The upshot is that the call object for that one particular invocation of the outer function continues to live, and the names and values of the function arguments and local variables persist in this object. JavaScript code cannot directly access the call object in any way, but the properties it defines are part of the scope chain for any invocation of the nested function. (Note that if an outer function stores global references to two nested functions, these two nested functions share the same call object, and changes made by an invocation of one function are visible to invocations of the other function.)
The object through which a method is invoked becomes the value of the this keyword within the body of the method.
While it is useful to think of
functions and methods differently, there is not actually as much difference between them as there initially appears to be. Recall that functions are values stored in variables and that variables are nothing more than properties of a global object. Thus, when you invoke a function, you are actually invoking a method of the global object. Within such a function, the
this keyword refers to the global object. Thus, there is no technical difference between functions and methods. The real difference lies in design and intent: methods are written to operate somehow on the
this object, while functions usually stand alone and do not use the
this object.
When a function
is invoked as a function rather that as a method, the
this keyword refers to the global object. Confusingly, this is true even when a nested function is invoked (as a function) within a containing method that was invoked as a method: the
this keyword has one value in the containing function but (counterintuitively) refers to the global object within the body of the nested function.
js 代码
- var o = {};
- o.m = function test(x) {
- alert(this);
- var y = x;
- this.y = x;
-
- (function() {
- alert(this);
- })();
-
- var z = function() {
- alert(this);
- };
- z();
-
- return function nestedTest() {
- alert(this);
- this.y = y + x;
- }();
- };
-
- o.m(4);alert(o.y);
- alert(window.y);
分享到:
相关推荐
《JavaScript犀牛书》是JavaScript编程领域的一本经典著作,被广大开发者誉为"骨灰级"的学习资料,尤其适合初学者入门。这本书全面而深入地介绍了JavaScript语言的核心概念、语法以及实际应用,旨在帮助读者建立起...
《JavaScript权威指南》是JavaScript编程领域的一本经典之作,由著名技术作家David Flanagan撰写,被誉为"JavaScript犀牛书"。这本书深入浅出地讲解了JavaScript语言的核心概念、语法特性和高级特性,对于想要全面...
JavaScript权威指南,最著名的javaScript参考用书。被大家亲切的成为犀牛书,是O'Reilly出版集团动物系列书的一分子。
犀牛JavaScript(第5版) JavaScript概述 语法结构 数据类型和值 变量 表达式和运算符 语句 对象和数组 函数 构造函数和原型 模块和名字空间 使用正则表达式的模式匹配 脚本化java 客户端 JavaScript
犀牛书,JavaScript书籍中的圣经。 本书是程序员学习核心JavaScript语言和由Web浏览器定义的JavaScript API的指南和综合参考手册。 第6版涵盖HTML 5和ECMAScript 5。很多章节完全重写,以便与时俱进,紧跟当今的最佳...
书中较全面地对 JavaScript 的各种函数以及特性作了介绍,而后面占了全书超过一半内容的 JavaScript 核心参考、客户端 JavaScript 参考、DOM 参考,无疑是编写 JavaScript 时最有力的参考手册(当然,在线的除外)。...
《犀牛JavaScript第5版》是一本深受程序员喜爱的JavaScript编程指南,对于深入理解和...通过阅读这本书,读者能够掌握JavaScript的精髓,无论是在Web开发、Node.js服务器端编程还是移动端应用开发中,都能游刃有余。
JavaScript权威指南(第六版)犀牛书,淘宝前端团队翻译,中文版
经典的JavaScript犀牛书!第6版特别涵盖了HTML5和ECMAScript5! 经典的JavaScript工具书,从1996年以来,本书已经成为JavaScript程序员心中的《圣经》。 程序员学习核心JavaScript语言和由Web浏览器定义的...
JavaScript权威指南,著名的犀牛书:) This Fifth Edition is completely revised and expanded to cover JavaScript as it is used in today's Web 2.0 applications. This book is both an example-driven ...
JavaScript 犀牛书,值得初学javascript一看
JavaScript 犀牛书 ,值得javascript初学者一看
JavaScript权威指南(第四版+中文版) 经典的JavaScript教材 犀牛书
《JS权威指南(第6版)2012版扫描版》是一本深入解析JavaScript语言的重量级著作,由著名编程专家Douglas Crockford等人撰写。这本书详细介绍了JavaScript的核心概念、语法和特性,旨在帮助读者全面理解并掌握这门广泛...
同时,书中可能还会讨论如何避免JavaScript的一些常见陷阱,比如作用域问题、异步编程的管理,以及如何优化JavaScript代码以提高性能。 总的来说,无论你是初学者还是有经验的开发者,"犀牛JavaScript第5版"都是一...
最新版本的javascript犀牛书,内涵HTML5
中文版 高清带标签扫描版pdf (淘宝前端团队倾情翻译!经典权威的JavaScript犀牛书!第6版特别涵盖了HTML5和ECMAScript5!)
JavaScript权威指南中文第六版 (犀牛书) 淘宝前端团队翻译