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

本文深入探讨了JavaScript中的变量声明、作用域、原始类型及引用类型等内容,并通过实例详细解析了全局变量与局部变量的区别,以及不同作用域下变量的行为特征。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 


 

转载于:https://www.cnblogs.com/microsheen/archive/2006/02/11/329054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值