Array.prototype.slice.call(arguments)

Array.prototype.slice.call(arguments)功能:将具有length属性的对象转成数组      //在IE8及之前的版本中无效 (因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换)

如:
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<div id="father">
			
		</div>
	</body>
	<script>
		var father=document.getElementById('father');
		for(var i=1;i<10;i++){
			var p=document.createElement('p');
			p.innerHTML=i;
			father.appendChild(p);
		}
		var childNodes=father.childNodes;
		// console.log(childNodes);
		console.log(convertToArray(childNodes));
		function convertToArray(nodes){
			var array=null;
			try{
				<span style="color:#FF0000;">array=Array.prototype.slice.call(nodes,0);</span>
			}
			catch(ex){
				<span style="color:#333399;"><strong>array=new Array();
				for(var i=0,len=nodes.length;i<len;i++){
					array.push(nodes[i]);
				}</strong></span>         //slice的内部实现
			}
			return array;
		}

	</script>
</html>

slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组。

 
再看call的用法,如下例子
1 var a = function(){
2      console.log(this);    // 'littledu'
3      console.log(typeof this);      //  Object
4      console.log(this instanceof String);    // true
5 }
6 a.call('zsn');

call可以改变当前函数的作用域。也就是改变this

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值