Created
August 28, 2013 01:37
-
-
Save hkongm/6361175 to your computer and use it in GitHub Desktop.
Array原生方法重要应用
This file contains 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 mergeTo = [4,5,6]; | |
var mergeFrom = [7,8,9]; | |
Array.prototype.push.apply(mergeTo, mergeFrom); | |
mergeTo; // is: [4, 5, 6, 7, 8, 9] |
This file contains 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
[ | |
{ name: "Robin Van PurseStrings", age: 30 }, | |
{ name: "Theo Walcott", age: 24 }, | |
{ name: "Bacary Sagna", age: 28 } | |
].sort(function(obj1, obj2) { | |
// Ascending: first age less than the previous | |
// 实现增序排列:前者的 age 小于后者 | |
return obj1.age - obj2.age; | |
}); | |
// Returns: | |
// [ | |
// { name: "Theo Walcott", age: 24 }, | |
// { name: "Bacary Sagna", age: 28 }, | |
// { name: "Robin Van PurseStrings", age: 30 } | |
// ] |
This file contains 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 nodesArr = Array.prototype.slice.call(document.querySelectorAll("div")); | |
// "true" array of DIVs | |
// 得到一个由 div 元素组成的“真正的”数组 | |
var argsArr = Array.prototype.slice.call(arguments); | |
// changes arguments to "true" array | |
// 把 arguments 转换成一个“真正的”数组 | |
var clone = myArray.slice(0); //浅克隆 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment