1. 数组结构: var arr = [1001, 'zs', 18, true];
2. 类数组对象结构: var obj = {0: 1001, 1: 'zs', 2: 18, 3: true, length: 4};
3. arguments对象结构:
function fun(){
var args = arguments;
}
fun(1001, 'zs', 18, true);
4. 类数组对象转为数组: var sliceObj = Array.prototype.slice.call(obj);
5. 例子
5.1. 代码
<!DOCTYPE html>
<html>
<head>
<title>类数组对象可以通过call、apply和bind方法转数组</title>
</head>
<body>
<script type="text/javascript">
var arr = [1001, 'zs', 18, true];
var obj = {0: 1001, 1: 'zs', 2: 18, 3: true, length: 4};
function fun(){
var args = arguments;
var sliceArguments = Array.prototype.slice.apply(arguments);
for(let item in sliceArguments){
document.write(sliceArguments[item] + '<br />');
}
}
fun(1001, 'zs', 18, true);
document.write('--------------------<br />');
// 类数组对象转为数组, 其它数组不行
var sliceObj = Array.prototype.slice.call(obj);
for(let item in sliceObj){
document.write(sliceObj[item] + '<br />');
}
document.write('--------------------<br />');
document.write(Array.prototype.slice.bind(obj)() + '<br />');
document.write('--------------------<br />');
document.write(Array.prototype.slice.bind(obj, 2)() + '<br />');
document.write('--------------------<br />');
document.write(Array.prototype.slice.bind(obj)(2));
</script>
</body>
</html>
5.2. 效果图