您的位置:首页 > Web前端 > JavaScript

Note Two : studying JavaScript: The Definitive Guide, 4th Edition

2006-02-11 21:49 465 查看
Chapter 4. Variables
1. Variable Declaration

please test the followings in browser.

<script language="javascript">
var a;
e = "I have a value";

//If you don't specify an initial value for a variable with the var statement,
//the variable is declared, but its initial value is undefined until your code stores a value into it.
document.write("a = ", a, "<br />");

//If you assign a value to a variable that you have not declared with var,
//JavaScript will implicitly declare that variable for you.
//however, that implicitly declared variables are always created as global variables,
//even if they are used within the body of a function.
document.write("e = ", e, "<br />");

//If you attempt to read the value of an undeclared variable, JavaScript will generate an error
//document.write("f = ", f, "<br />"); //it will throw an exception if you run it.
</script>

the result shoule be:
a = undefined
e = I have a value

2. Variable Scope
1)JavaScript will implicitly declare that variable for you. and that implicitly declared variables are always created as global variables,
2) JavaScript does not have block-level scope. All variables declared in a function, no matter where they are declared, are defined throughout the function.
but this rule can cause surprising results. The following code illustrates this:

var scope = "global";
function f( ) {
alert(scope); // Displays "undefined", not "global"
var scope = "local"; // Variable initialized here, but defined everywhere
alert(scope); // Displays "local"
}
f( );

3. Primitive Types and Reference Types
1) Numbers, boolean, null, undefined
these types have a fixed size in memory. the variable can directly hold any primitive value

2) Strings
strings are immutable: there is no way to change the contents of a string value. strings as an immutable reference type that behaves like a primitive type or as a primitive type implemented with the internal efficiency of a reference type.

3) reference types
they do not have a fixed size. the variable stores a reference to the value. Typically, this reference is some form of pointer or memory address.

4. Variables as Properties
Variables in JavaScript are fundamentally the same as object properties.
1) The Global Object
before executing any JavaScript code, is create a global object. The properties of this object are the global variables of JavaScript programs.

In top-level code (i.e., JavaScript code that is not part of a function), you can use the JavaScript keyword this to refer to the global object.

In client-side JavaScript, the Window object serves as the global object for all JavaScript code contained in the browser window it represents.

2) Local Variables
While the body of a function is executing, the function arguments and local variables are stored as properties of this call object

3) JavaScript Execution Contexts
Each time the JavaScript interpreter begins to execute a function, it creates a new execution context for that function.

JavaScript code that is not part of any function runs in an execution context that uses the global object for variable definitions. And every JavaScript function runs in its own unique execution context with its own call object in which local variables are defined.

5. Variable Scope Revisited
1) In top-level JavaScript code (i.e., code not contained within any function definitions), the scope chain consists of a single object
2) In a (non-nested) function, however, the scope chain consists of two objects. The first is the function's call object, and the second is the global object.
3)A nested function would have three or more objects in its scope chain.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: