标签:item rgs lan his har tag function apply call
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
<li class="item">5</li>
<li class="item">6</li>
<li class="item">7</li>
<li class="item">8</li>
</body>
</html>
<script type="text/javascript">
var args=[1,2];
function test(){
console.log(arguments);
}
Array.prototype.push.call(args,3,4);
test(...args);
//利用Array.prototype.push.call()方法向args数组中插入了3和4,并利用Es6的延展操作符(...)将数组展开并传入test
//所以console为1,2,3,4
var obj={
myName:‘john‘,
getName:function(){
return this.myName;
}
};
var obj2={
myName:‘xiaoming‘
}
var nameFn=obj.getName;
console.log(nameFn.apply(obj2));
//将obj方法赋值给了变量nameFn 通过 apply方法将this指向obj2对象,所以最终结果为 xiaoming
var elem=document.getElementsByTagName(‘li‘);
console.log(elem.length);
// for(var i=0;i<elem.length;i++){
// (function(w){
// elem[w].onclick=function(){
// alert(w);
// }
// })(i)
// }
//闭包
for(var i=0;i<elem.length;i++){
(function(w){
elem[w].onclick=function(){
alert(w);
}
})(i)
}
</script>
标签:item rgs lan his har tag function apply call
原文地址:http://www.cnblogs.com/johnchenwei/p/6134296.html