Array原生方法
push()末尾推入元素 返回數組長度
pop()末尾彈出元素 返回彈出元素
shift()起始彈出元素 返回彈出元素
unshift()起始推入元素 返回數組長度
代碼如:
var arr1 = ["b","c","d","e"];
var arr2 = arr1.push("f");//arr1:["b","c","d","e","f"], arr2:5
var arr3 = arr1.pop();//arr1:["b","c","d","e"], arr3:"f"
var arr4 = arr1.unshift("a");//arr1:["a","b","c","d","e"], arr4:5
var arr5 = arr1.shift();//arr1:["b","c","d","e"], arr5:"a"
?
Ecmascript5有擴展數組原型方法forEach,filter等
if(typeof Array.prototype.forEach !== "function"){
Array.prototype.forEach = function(fn,thisObj){
var scope = thisObj || window;
for(var i=0, len=this.length; i<len; i++){
fn.call(scope,this[i],[i],this);
}
}
};
if(typeof Array.prototype.filter!== "function"){
Array.prototype.filter= function(fn,thisObj){
var scope = thisObj || window;
var a = [];
for(var i=0, len=this.length; i<len; i++){
if(!fn.call(scope,this[i],[i],this)){
continue;
}
a.push(this[i])
}
return a;
}
}
?
?
轉載于:https://www.cnblogs.com/samKR/p/3794493.html
總結
- 上一篇: 今天你多态了吗? 【转】
- 下一篇: 基于html5背景图片自适应代码