091_类数组对象转为数组

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值