Javascript Variables

要学好JS,要从基础学起。翻了一下《Javascript权威指南第五版》,英文的。看得有点模糊,还是勉强看完了变量这一小章。用我有限的英文翻译了一点,不要恶心我。

1、Javascript变量作用域
     JavaScript的变量作用域分为全局和局部两种。

代码
1 var scope = "global"; // 声明一个全局变量
2  function checkscope( ) {
3 var scope = "local"; // 声明一个同名的局部变量
4   document.write(scope); // 使用了局部而非全局变量
5  }
6 checkscope( ); // 输出"local"
2、var的作用
     Javascript变量可以不用声明,但是你没有使用var声明一个变量,可能会出现问题:

代码
1 scope = "global"; // 声明一个全局变量,没有使用var修饰
2  function checkscope( ) {
3 scope = "local"; // 改变全局变量
4 document.write(scope); // 使用全局变量
5 myscope = "local"; // 声明一个新的全局变量
6 document.write(myscope); // 使用新的全局变量
7 }
8 checkscope( ); // 输出"locallocal"
9 document.write(scope); // 这次输出 "local"
10 document.write(myscope); // 这次输出 "local"
3、原始类型(primitive types )和引用类型(reference types)
     Numbers, boolean values, and the null and undefined types are primitive.Objects, arrays, and functions are reference types.
     两者的区别:

代码
var a = 3.14; // 初始化变量的值
var b = a; // 复制变量的值赋予新变量
a = 4; // 更改原始变量的值
alert(b) // 变量3.14; 复制变量的值未变

var a = [1,2,3]; // 初始化一个引用类型的值
var b = a; // 复制引用类型的值
a[0] = 99; // 更改原始变量的值
alert(b); // 数组改变为[99,2,3] 使用新的引用值
4、回收机制(Garbage Collection)

    翻译:引用类型没有固定的大小;确实,他们可能会占用很大内存。像我们之前讨论的,变量并非存储引用变量的值,它的值存储在其他的地方,变量只是一个指针作用。因为strings, objects,Arrays 等没有固定大小,他们是动态分配内存的。当他们的大小确定时,当程序创建时(strings, objects,Arrays ),解释器就会分配内存来存储实体。像这样动态分配内存,它最终都是空出来重用的,或者JavaScript解释器把所有可用变量内存销毁。不像C、C++那样,内存要自动清除。这是程序员要做的事,当它们不再需要时,对于程序员来说是一个繁重的任务也是bugs的来源。 代替人工重新分配存储地址,JavaScript依赖一种叫垃圾收集的技术。JS解释器会判断对象是否不再使用,当它不访问的时候就判定了该对象不被使用,它的内存将被重新分配。
     考虑下面的例子:

var s = "hello"; // Allocate memory for a string
var u = s.toUpperCase( ); // Create a new string
s = u; // Overwrite reference to original string

     After this code runs, the original string "hello" is no longer reachable; there are no references to it in any variables in the program. The system detects this fact and frees up its storage space for reuse.

    Garbage collection is automatic and is invisible to the programmer. You need to know only enough about garbage collection to trust that it works: to know you can create all the garbage objects you want, and the system will clean up after you!

5、Variable Scope Revisited

Figure4-1


    大概解释这个图,当发现变量z,解释器是这样工作的:首先在当前g()中查找z是否已定义,如果定义了就返回g()中定义z的值。如果未定义,就向上层查找,f()中是否已定义,如果一直向上找,到全局变量都没有定义过z,就会返回undefined。

ps:undefined和unassigned区别:

The examples in the previous section demonstrate a subtle point in JavaScript programming: there are two different kinds of undefined variables. The first kind of undefined variable is one that has never been declared. An attempt to read the value of such an undeclared variable causes a runtime error. Undeclared variables are undefined because they simply do not exist. As described earlier, assigning a value to an undeclared variable does not cause an error; instead, it implicitly declares the variable in the global scope.

 

The second kind of undefined variable is one that has been declared but has never had a value assigned to it. If you read the value of one of these variables, you obtain its default value, undefined. This type of undefined variable might more usefully be called unassigned, to distinguish it from the more serious kind of undefined variable that has not even been declared and does not exist.

 

The following code fragment illustrates some of the differences between truly undefined and merely unassigned variables:

var x; // Declare an unassigned variable. Its value is undefined.
alert(u); // Using an undeclared variable causes an error.
u = 3; // Assigning a value to an undeclared variable creates the variable.

 

posted @ 2010-09-28 14:03  e.e.p  阅读(332)  评论(0编辑  收藏  举报