<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Document</title>
<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>
<script type= "Text/javascript" >
var args=[1,2];
function Test () {
Console.log (arguments);
}
Array.prototype.push.call (args,3,4);
Test (.... args);
Use the Array.prototype.push.call () method to insert 3 and 4 into the args array and take advantage of the ES6 extension operator (...). Expand the array and pass in the test
So the console is 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));
The Obj method is assigned to the variable namefn by using the Apply method to point this to the Obj2 object, so the end result is 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)
// }
Closed Package
for (Var i=0;i<elem.length;i++) {
(function (W) {
Elem[w].onclick=function () {
Alert (w);
}
}) (i)
}
</script>
Application of closures