数组与对象/字符串的类同结构

1、arguments 是一个类数组对象,转化成数组

Array.prototype.slice.apply(arguments)

2、字符串是一个类数组对象:他的字符相当于数组的元素

var str = "abcdef";
console.log(str[0]) //a
ES6中数组扩展运算...
var
str = "abcdef"; var arr = [...str]; console.log(arr); // ["a", "b", "c", "d", "e", "f"] console.log(arr[0]); //a

3、一切皆是对象,数组也是对象发展而来

4、选用数组或对象规则:对排序有要求时使用数组,否则使用对象,数组是为了有序才出现的

1)当对象的属性名为有序数字:

var obj1 = {
    "0":"zhansan",
    "1":"lisi",
    "2":"wangwu"
}
console.log(obj1[0]) //
zhansan
var arr1 = ["zhansan","lisi","wangwu"];

2)当数组的下标为字符串:

var arr2 = new Array();
arr2['a'] = [{name:"Adina",age:1},{name:"阿    化",age:6}];
arr2['b'] = [{name:"波拉",age:3},{name:"博深",age:4}];
arr2['c'] = [{name:"曹雪芹",age:5},{name:"曹操",age:6}];
var obj2 = {
    a: [{name:"Adina",age:1},{name:"阿化",age:6}],
    b: [{name:"波拉",age:3},{name:"博深",age:4}],
    c: [{name:"曹雪芹",age:5},{name:"曹操",age:6}]
}

你可能感兴趣的:(数组与对象/字符串的类同结构)