javascript高级程序设计学习笔记

来源:互联网 发布:河北南网 交易数据 编辑:程序博客网 时间:2024/04/30 02:47

1.javascript中的case语句

Although the  switch statement was borrowed from other languages, it has some unique characteristics in ECMAScript. First, the switch statement works with all data types (in many languages it works only with numbers), so it can be used with strings and even with objects. Second, the case values need not be constants; they can be variables and even expressions.

javascript中的case语句适用于所有的数据类型,不像其他语言(C中只能适用于number和enum)只能使用特定的类型;javascript中case语句后面可以是非常量。

switch (“hello world”) {    case “hello” + “ world”:         alert(“Greeting was found.”);        break;    case “goodbye”:         alert(“Closing was found.”);        break;    default:         alert(“Unexpected message was found.”);}

The  switch statement compares values using the identically equal operator, so no type coercion occurs (for example, the string "10" is not equal to the number 10).

switch中的判断使用的是恒等,因此在处理时没有类型转换,例如:10恒不等于"10"

2.javascript中没有函数重载

ECMAScript functions cannot be overloaded in the traditional sense. In other languages, such as Java, it is possible to write two definitions of a function so long as their signatures (the type and number of arguments accepted) are different. As just covered, functions in ECMAScript don’t have signatures, because the arguments are represented as an array containing zero or more values. Without function signatures, true overloading is not possible.If two functions are defi ned to have the same name in ECMAScript, it is the last function that becomes the owner of that name.

JavaScript不像其他语言,它没有函数重载。在Java中,当两个函数的函数签名不一样的时候,两个函数满足重载。在JavaScript中,当两个函数有同样的名称时,后面的函数会覆盖掉前面的函数。

function addSomeNumber(num){    return num + 100;}                   function addSomeNumber(num) {    return num + 200;}                   var result = addSomeNumber(100);    //300

3.基本语法总结
The following are some of the basic elements of ECMAScript:

I.The basic data types in ECMAScript are Undefi ned, Null, Boolean, Number, and String.

II.Unlike other languages, there’s no separate data type for integers versus fl oating-point values; the Number type represents all numbers.

III.There is also a complex data type, Object, that is the base type for all objects in the language.

IV.A strict mode places restrictions on certain error-prone parts of the language.

V.ECMAScript provides a lot of the basic operators available in C and other C-like languages, including arithmetic operators, Boolean operators, relational operators, equality operators, and assignment operators.

VI.The language features fl ow-control statements borrowed heavily from other languages, such as the  if statement, the  for  statement, and the  switch statement.

Functions in ECMAScript behave differently than functions in other languages:

I.There is no need to specify the return value of the function since any function can return any value at any time.
II.Functions that don’t specify a return value actually return the special value undefined. There is no such thing as a function signature, because arguments are passed as an array containing zero or more values.
III.Any number of arguments can be passed into a function and are accessible through the arguments object.
IV.Function overloading is not possible because of the lack of function signatures.

4.JavaScript中的作用域

JavaScript’s lack of block-level scopes is a common source of confusion. In other C-like languages, code blocks enclosed by brackets have their own scope (more accurately described as their own execution context in ECMAScript), allowing conditional definition of variables. For example, the following code may not act as expected:

if (true) {    var color = “blue”;}                   alert(color);    //”blue”

Here, the variable color is defi  ned inside an  if statement. In languages such as C, C++, and Java, that variable would be destroyed after the  if statement is executed. In JavaScript, however, the variable declaration adds a variable into the current execution context (the global context, in this case).

JavaScript中没有块作用域,在块中定义的变量成为“上一级”的作用域变量。

//在JavaScript中,这段代码运行正确
for
(var i=0; i < 10; i++){ doSomething(i);} alert(i); //10 i成为当前作用域中的变量。

When a variable is declared using var , it is automatically added to the most immediate context available. In a function, the most immediate one is the function’s local context; in a  with statement, the most immediate is the function context. If a variable is initialized without fi  rst being declared, it gets added to the global context automatically, as in this example:

function add(num1, num2) {    var sum = num1 + num2;    return sum;}                   var result = add(10, 20);  //30alert(sum);                //causes an error since sum is not a valid variable
function add(num1, num2) {    sum = num1 + num2;    return sum;}                   var result = add(10, 20);  //30alert(sum);                //3 sum成为全局变量

5.GARBAGE COLLECTION(垃圾回收)

I.Mark-and-Sweep(标记-清除)

When a variable comes into context, such as when a variable is declared inside a function, it is fl agged as being in context. Variables that are in context, logically, should never have their memory freed, because they may be used as long as execution continues in that context. When a variable goes out of context, it is also fl agged as being out of context.

When the garbage collector runs, it marks all variables stored in memory (once again, in any number of ways). It then clears its mark off of variables that are in context and variables that are referenced by in-context variables. The variables that are marked after that are considered ready for deletion, because they can’t be reached by any in-context variables. The garbage collector then does a memory sweep , destroying each of the marked values and reclaiming the memory associated with them.

II.Reference Counting(引用计算)

The idea is that every value keeps track of how many references are made to it. When a variable is declared and a reference value is assigned, the reference count is one. If another variable is then assigned to the same value, the
reference count is incremented. Likewise, if a variable with a reference to that value is overwritten with another value, then the reference count is decremented. When the reference count of a value reaches zero, there is no way to reach that value and it is safe to reclaim the associated memory. The garbage collector frees the memory for values with a reference count of zero the next time it runs.

Reference counting was initially used by Netscape Navigator 3.0 and was immediately met with a serious issue: circular references. A  circular reference  occurs when object A has a pointer to object B and object B has a reference to object A, such as in the following example:

function problem(){    var objectA = new Object();    var objectB = new Object();                       objectA.someOtherObject = objectB;    objectB.anotherObject = objectA;}
In this example, objectA and objectB reference each other through their properties, meaning that each has a reference count of two. In a mark-and-sweep system, this wouldn’t be a problem because both objects go out of scope after the function has completed. In a reference-counting system, though,  objectA and objectB will continue to exist after the function has exited, because their reference counts will never reach zero. If this function were called repeatedly, it would lead to a large amount of memory never being reclaimed. For this reason, Netscape abandoned a reference-counting garbage-collection routine in favor of a mark-and-sweep implementation in version 4.0. Unfortunately, that’s not where the reference-counting problem ended.

引用计数方式在循环应用的情况下,会发生内存泄露.

6.Chapter 4: Variables, Scope, and Memory

Summary

Two types of values can be stored in JavaScript variables: primitive values and reference values. Primitive values have one of the five primitive data types: Undefi ned, Null, Boolean, Number, and String. Primitive and reference values have the following characteristics:

➤Primitive values are of a fi xed size and so are stored in memory on the stack.
➤Copying primitive values from one variable to another creates a second copy of the value.
➤Reference values are objects and are stored in memory on the heap.
➤A variable containing a reference value actually contains just a pointer to the object, not the object itself.
➤Copying a reference value to another variable copies just the pointer, so both variables end up referencing the same object.
➤The  typeof operator determines a value’s primitive type, whereas the  instanceof operator is used to determine the reference type of a value.
0 0