Javascript closure Solution

Source: Internet
Author: User

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<SCRIPT type = "text/JavaScript">

/* The following is taken from the prototype framework. Code ---------------------------------------*/

VaR $ A = function (iterable ){
If (! Iterable) return [];
If (iterable. toarray ){
Return iterable. toarray ();
} Else {
VaR Results = [];
For (VAR I = 0, length = iterable. length; I <length; I ++)
Results. Push (iterable [I]);
Return results;
}
}

// Closure method (the first parameter is the object of the binding method, and the following parameter is the parameter of the binding method ).
Function. Prototype. Bind = function (){
VaR _ method = This, argS = $ A (arguments), object = args. Shift ();
Return function (){
Return _ method. Apply (object, argS. Concat ($ A (arguments )));
}
}

/*-------------------------------------------------------------------*/

Function node (ID, name, CN ){
This. ID = ID;
This. Name = Name;
This.cn = cn;
}
VaR nodes = new array ();
Nodes [0] = new node (1, "node1", "Node 1 ");
Nodes [1] = new node (2, "node2", "Node 2 ");

VaR nodeconfig = {};
Nodeconfig. ticks = 4*1000; // refresh frequency
Nodeconfig. MSG = "running ";

Function showstatus (node ){
Alert (node. Name + ":" + nodeconfig. msg );
}

Function start (){
VaR Len = nodes. length;
For (VAR I = 0; I <Len; I ++ ){
// Closure.
VaR bindfun = showstatus. BIND (null, nodes [I]);
Setinterval (bindfun, nodeconfig. ticks );
}
}

Start ();


</SCRIPT>

</Head>
<Body>
<Input id = "button1" type = "button" value = "button" onclick = "TT ();"/>
</Body>
</Html>

ReferenceArticle: Http://www.cnblogs.com/KevinYang/archive/2009/07/14/1522915.html

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.