This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var str = 'abcde'; | |
| var obj = new String(str); | |
| function newToString() { | |
| return 'hello, world!'; | |
| } | |
| function func(val) { | |
| val.toString = newToString; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 声明变量 str 和 num | |
| var str = 'test'; | |
| var num = 3 + 2 - 5; | |
| // 声明变量 n | |
| for (var n in Object) { | |
| // ... | |
| } | |
| // 声明变量 i, j, k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Shallow copy(浅拷贝) | |
| var newObject = $.extend({}, oldObject); | |
| // Deep copy(深拷贝) | |
| var newObject = $.extend(true, {}, oldObject); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var array1 = [0, 1, 2]; | |
| var array2 = array1.slice(0); // copy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function foo(bar) { | |
| bar = { x: 2 }; | |
| } | |
| var a = { x: 1, y: 7 }; | |
| foo(a); | |
| alert(a.x); // still 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function foo(bar) { | |
| bar.x = 2; | |
| } | |
| var a = { x: 1, y: 7 }; | |
| foo(a); | |
| alert(a.x); // => 2 | |
| var a = { x: 1, y: 7 }; | |
| var b = a; | |
| b.x = 2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var a = { x: 1, y: 7 }; | |
| var b = { x: 1, y: 7 }; | |
| if (a == b) // => false | |
| if (a === b) // => false | |
| b = a; | |
| if (a == b) // => true | |
| if (a === b) // => true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var point = { x: 1, y: 7 }; | |
| point.x = 2; | |
| point.y = 9; | |
| function fn() { | |
| return true; | |
| }; | |
| fn.x = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // sample 1 | |
| function foo(bar) { | |
| bar = 2; | |
| } | |
| var a = 1; | |
| foo(a); | |
| alert(a); // => 1 | |
| // sample 2 | |
| var a = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var a = "Roger"; | |
| var b = "Roger"; | |
| if (a == b) // => true | |
| if (a === b) // => true |