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) 英文mobi 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
<<Secrets of the JavaScript Ninja>>英文原版
Secrets of the JavaScript Ninja(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Secrets of the JavaScript Ninja,Javascript忍者禁术英文原版高清pdf下载。
JavaScript书籍包括ppk谈JavaScript.pdf+JavaScript语言精粹(修订版).pdf+Secrets of the JavaScript Ninja.pdf。
Secrets Of The JavaScript Ninja
jQuery作者John Resig写的书
Secrets of the JavaScripot Ninja, 2nd Edition
Secrets of the JavaScript Ninja 2nd 英文pdf
Secrets of the JavaScript Ninja (Manning MEAP 2009) pdf 电子书
Secrets of the JavaScript Ninja, 2nd Edition
学习JavaScript进阶工具。从小白到高玩,成就JS高手。
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 ...