美文网首页前端面试基础必备JS学习笔记
JS 类数组对象arguments和数组对象的区别

JS 类数组对象arguments和数组对象的区别

作者: puxiaotaoc | 来源:发表于2018-09-13 18:30 被阅读15次

一、类数组对象的定义:

      1)拥有length属性,其它属性(索引)为非负整数(对象中的索引会被当做字符串来处理);
      2)不具有数组所具有的方法;
      javascript中常见的类数组有 arguments 对象和 DOM 方法的返回结果,比如
document.getElementsByTagName() ;

// 类数组示例 1
  var nodeList = document.getElementsByTagName('ul');
  console.log(nodeList instanceof Array); // false 类数组
  console.log(nodeList[0]); // <ul>...</ul>
//类数组示例 2
var a = {'1':'gg','2':'love','4':'meimei',length:5};
Array.prototype.join.call(a,'+');//'+gg+love++meimei'

//非类数组示例
var c = {'1':2};   //没有length属性就不是类数组
//类数组示例 3
  var obj = {a:1};
  function process(obj){
    obj={};
  }
  console.log(obj); // {a: 1}
//类数组示例 4
function b(x, y, a) {
    arguments[2] = {"name" : "func"};
    alert(a.name);    //"func"
}
b(1, 2, {"name" : "win"});
//类数组示例 5
  function b(x,y,a){
    var arr = [x,y,a];
    arr[2]={
      "name":"func"
    };
    alert(a.name);
  }
  b(1,2,{"name":"win"}); // win
// length是数组的一个内置属性
// 对象以键值对存取数据
// 数组通过索引来存取数据
  var obj = {};
  var arr = [];
  obj[2] = 'a';
  arr[2] = 'a';
  console.log(obj); // {2: "a"}
  console.log(arr); // [empty × 2, "a"]
  console.log(obj.length); // undefined
  console.log(arr.length); // 3
// obj并不具有length属性
  var obj ={
    name: 'aa',
    age: 24
  }
  console.log(obj.length); // undefined
// auguments是伪数组
(function() {
    console.log(typeof arguments); // object
}());
// 在DOM对象中,childNodes也是伪数组
console.log(typeof document.body.childNodes); // object

二、类数组转换成数组

// 类数组转换成数组
Array.prototype.slice.call(arrayLike)
var toArray = function(s){  
   try {  
       return Array.prototype.slice.call(s);  
   } catch(e){  
       var arr = [];  
       for(var i = 0,len = s.length; i < len; i++){  
          //arr.push(s[i]);  
          arr[i] = s[i];     //据说这样比push快
       }  
       return arr;  
    } 
}
   Function.prototype.my_bind = function() {
      var self = this, // 保存原函数
        context = Array.prototype.shift.call(arguments), // 保存需要绑定的this上下文
        // 上一行等价于 context = [].shift.call(arguments);
        args = Array.prototype.slice.call(arguments); // 数组转换为类数组
      return function() { // 返回一个新函数
        self.apply(context, Array.prototype.concat.call(args, Array.prototype.slice.call(arguments)));
      }
    }

  function a(m, n, o) {
    console.log(this.name + ' ' + m + ' ' + n + ' ' + o);
  }

  var b = {
    name: 'kong'
  };

  a.my_bind(b, 7, 8)(9); // kong 7 8 9

参考链接:
js类数组转数组的方法(ArrayLike)

1

](https://www.cnblogs.com/guorange/p/6668440.html)

相关文章

  • 类数组、arguments

    arguments为一个类数组的对象;类数组对象即为:有length属性和索引的对象。一、类数组===》数组var...

  • JS 类数组对象arguments和数组对象的区别

    一、类数组对象的定义: 1)拥有length属性,其它属性(索引)为非负整数(对象中的索引会被当做字符串来处理);...

  • 类数组对象与arguments

    原文出处 JavaScript深入之类数组对象与arguments 类数组对象 所谓的类数组对象: 拥有一个 le...

  • Javascript中的arguments

    arguments类数组对象: 参考资料1.arguments类数组对象是一种特殊的对象,它有数组的一部分属性(比...

  • JS数组以及数组变换

    有关数组 数组对象——一种特殊的对象JS其实没有数组,只使用对象来模拟数组 典型数组和JS数组的区别 典型数组 元...

  • [].shift.call(arguments)

    [].shift.call(arguments) 把类数组对象转为数组对象,删除并拿到arguments的第一项。...

  • javascript学习笔记--arguments类数组对象

    调用函数时浏览器都会传两个对象,this和arguments。1.arguments一个类数组对象,但不是数组, ...

  • ES6 Array

    类数组、可遍历 对象(arguments selector返回的对象)转成数组 Array.from() Arra...

  • JS 数组 和 类数组

    类数组是一个普通对象,而真实的数组是 Array 类型 arguments 也是类数组 类数组转换为数组

  • JavaScript深入之类数组对象与arguments

    JavaScript深入系列第十三篇,讲解类数组对象与对象的相似与差异以及arguments的注意要点 类数组对象...

网友评论

    本文标题:JS 类数组对象arguments和数组对象的区别

    本文链接:https://www.haomeiwen.com/subject/hnzvgftx.html