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. 效果图

本文介绍了JavaScript中数组结构、类数组对象结构以及arguments对象的使用。通过示例展示了如何将类数组对象转换为数组,利用Array.prototype.slice方法及其call、apply和bind方法。同时,代码演示了在函数内部获取并处理arguments对象,以及通过bind方法进行切片操作的多种方式。
324

被折叠的 条评论
为什么被折叠?



