JavaScript数组(二)

188 阅读1分钟

concat 整合两个数组

var arr3 = arr1.concat(arr2)

toString()

slice方法,截取数组 [start, end)  

join/split

join将数组用参数里的字符连接成字符串

split将字符串按照参数1的字符分开,截取参数2个放到数组里


类数组

类数组有length属性,有类似数组的0、1、2等index为key  document.getElementsByTagName()语句返回的就是一个类数组对象。在function调用中,function代码内的arguments变量(保存传入的参数)也是一个类数组对象


var obj = {
    '2': 3,
    '3': 4,
    'length': 2,
    'slilce': Array.prototype.splice,
    'push': Array.prototype.push
}
obj.push(1);
obj.push(2);
console.log(obj)  //{ '2': 1,
                  //'3': 2,
                  //length: 4,
                  //slilce: [Function: splice],
                  //push: [Function: push] }

Array.prototype.push = function(elem){

      this[this.length] = elem;

      this.length++;

}