javascript权威指南(03~06)
3.12.自动类型转换
String | Number | Boolean | Object | |
---|---|---|---|---|
Undefined value | "undefined" | NaN | false | Error |
null | "null" | 0 | false | Error |
Nonempty string | As is | Numeric value of string or NaN | TRue | String object |
Empty string | As is | 0 | false | String object |
0 | "0" | As is | false | Number object |
NaN | "NaN" | As is | false | Number object |
Infinity | "Infinity" | As is | true | Number object |
Negative infinity | "-Infinity" | As is | TRue | Number object |
Any other number | String value of number | As is | true | Number object |
true | "true" | 1 | As is | Boolean object |
false | "false" | 0 | As is | Boolean object |
Object | toString( ) | valueOf( ), toString( ), or NaN | true | As is |
3.13. 简单数据进行运算的时候,会短暂的转换成相应的对象,因为相关方法是定义在对象上面的,运算完了,这个对象也马上销毁。
var s = "hello world"; // A primitive string value
var S = new String("Hello World"); // A String object
typeof运算会返回不同的结果
3.14.对象进行数值运算的时候,通过valueOf() ,toString()进行运算。但是在当对象是Date的+时候有个例外,优先toString()
3.15.4. 传值与传址
Type | Copied by | Passed by | Compared by |
---|---|---|---|
number | Value | Value | Value |
boolean | Value | Value | Value |
string | Immutable | Immutable | Value |
object | Reference | Reference | Reference |
4.2.1.变量声明
在函数中未通过var声明的变量,且全局也没这变量,程序会自行声明成一个全局变量,所以变量声明,需要var;
function a() { b=10; } a(); //window.b=10;
4.3.1.没有块级作用域
主要是要了解js的执行顺序,js是先解释,再执行的,解释的时候会先看见相关的var定义操作。
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(); //这是另一个例子 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.
5.10.4.delete操作
涉及到属性的时候,操作只是删除相关的引用,不会删除所指向的对象
var o = {x:1, y:2}; // Define a variable; initialize it to an object delete o.x; // Delete one of the object properties; returns true typeof o.x; // Property does not exist; returns "undefined" delete o.x; // Delete a nonexistent property; returns true delete o; // Can't delete a declared variable; returns false修正好像IE8和FF都显示为true,可删除 delete 1; // Can't delete an integer; returns true IE8提示错误,FF为returns true x = 1; // Implicitly declare a variable without var keyword delete x; // Can delete this kind of variable; returns true x; // Runtime error: x is not defined
6.9. for/in
for/in循环体中删除还未列举的属性,则该属性不会再被列举
for/in循环体中添加属性,则根据情况可能列举添加的属性,可能是由于列举是无顺序的导致的
for/in不会列举所有属性。
6.15. return
函数通过return;或执行完返回的是 undefined (void 0; 同样)