Application of closures

Source: Internet
Author: User

<! 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.