Chapter 3
Scoping and functions
Scoping rules in Javascript:
1. Variable declarations are in scope from their point of declaration to the end of the function within which they are declared, regardless of block nesting.
Look at this example:
if(window){
var x = 13;
}
alert(x); // 13
2. Named functions are in scope within the entire function within which they are declared, regardless of block nesting.
function outer {
alert(typeof inner === 'function', "inner() in scope before declaration");
function inner() { return "inner";}
alert(typeof inner === 'function', "inner() in scope after declaration");
}
3. For the purposes of declaration scopes, the global context acts like one big function encompassing the code on the page.
And for the code snippet below, what's the scope for each function and variable?
function outer(){
var a = 1;
function inner(){ /* does nothing */ }
var b = 2;
if (a == 1) {
var c = 3;
}
}
outer();
Answer:
Invocations
There are actually four different ways to invoke a function, each with their own nuances.
They are:
1. As a function, in which the function is invoked in a straightforward manner.
2. As a method, which ties the invocation to an object, enabling object-oriented
programming.
3. As a constructor, in which a new object is brought into being.
4. Via their apply() or call() methods
Difference between 1 & 2:
When using 2, *this* scope is the caller.
How is 3 implemented?
Invoking a function as a constructor is a powerful feature of JavaScript because when a
constructor is invoked, the following special actions take place:
A new empty object is created.
This object is passed to the constructor as the this parameter, and thus becomes the
constructor’s function context.
In the absence of any explicit return value, the new object is returned as the
constructor’s value.
Summary:
o When invoked as a simple function, the context is the global object
(window).
o When invoked as a method, the context is the object owning the method.
o When invoked as a constructor, the context is a newly allocated object.
o When invoked via the apply() or call() methods of the function, the
context can be whatever the heck we want.
- 大小: 25.9 KB
分享到:
相关推荐
In Secrets of the JavaScript Ninja, JavaScript expert John Resig reveals the inside know-how of the elite JavaScript programmers. Written to be accessible to JavaScript developers with intermediate-...
Secrets of the JavaScript Ninja(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Secrets of the JavaScript Ninja(2nd) 英文mobi 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
“Secrets of the JavaScript Ninja”(JavaScript忍者秘籍)是John Resig的作品,他是jQuery库的创始人。这本书深入探讨了JavaScript的高级技术,包括函数式编程、模块化、异步编程和性能优化,旨在将读者提升到...
《Secrets Of The JavaScript Ninja》这本书由John Resig和Bear Bibeault共同撰写,是Manning出版社出版的一本深入探讨JavaScript高级技术和最佳实践的专业书籍。本书面向已经具备一定JavaScript基础的开发者,旨在...
### JavaScript Ninja Secrets: Key Insights from "Secrets of the JavaScript Ninja, 2nd Edition" In the world of web development, JavaScript has become the lingua franca, powering applications across ...
《Secrets of the JavaScript Ninja》第二版是John Resig、Bear Bibeault、Josip Maras合著的一本专注于高级JavaScript编程技巧的书籍。第二版涉及了ECMAScript 6 (ES6)中的新特性,以及如何使用它们来编写更加优雅...
《Secrets of the JavaScript Ninja》是一本深入探讨JavaScript高级编程技术的书籍,由Manning Publications出版。本书旨在帮助读者掌握如何编写高效且跨浏览器兼容的JavaScript代码。在实际开发过程中,除了面对...
John Resig is an acknowledged JavaScript authority and the creator of the jQuery library. Bear Bibeault is a web developer and coauthor of Ajax in Practice, Prototype and Scriptaculous in Action, and ...