类数组
- 类数组必须有length属性
- 类数组继承于Object.prototype。而数组继承于Array.prototype
function isArrayLike(o) {
if (o &&
typeof o === 'object' &&
isFinite(o.length) &&
o.length >= 0 &&
o.length===Math.floor(o.length) &&
o.length < 4294967296)
return true;
else
return false;
}
示例
var arrLike = {
'0' : 'a',
'1' : 'b',
'2' : 'c',
'3' : 'd',
'4' : 'e',
length : 5
}
console.log(arrLike);
arguments
- 是function的一个属性
- 是函数内部的参数值的实参列表,是一个类数组对象,内置的局部变量
- 非箭头函数有arguments属性
- 形参和实参有对应关系
function test (a){
console.log(a);
arguments[0] = 100;
console.log(a,arguments[0])
console.log(Object.prototype.toString.call(arguments))
}
test(10);
arguments共享关系的解除
function test(a){
'use strict';
console.log(a,arguments[0]);
a = 100;
console.log(a,arguments[0]);
}
test(10);
function test(a = 1){
console.log(a);
arguments[0] = 100;
console.log(a,arguments[0]);
}
test(10);
function test(...args){
console.log(args,arguments);
arguments[0] = 100;
arguments[1] = 200;
console.log(args,arguments);
}
test(1, 2);
function test({a,b}){
console.log(a);
console.log(b);
console.log(arguments)
arguments[0] = 100;
arguments[1] = 200;
console.log(a);
console.log(b);
console.log(arguments);
}
test({a:1,b:2});