The BIND function, as the name implies, is used to bind a scope to the call function, because this is easy to lose its original scope and direct to the top-Layer window object. For more information, see my other blog post "dynamic binding of JavaScript this with dynamic binding". This article focuses on designing a non-intrusive binding function.
Window. name = "The Window object" function scopetest () {return this. name} // calling the function in global scope: scopetest () //-> "The Window object" Var Foo = {name: "The Foo object! ", Otherscopetest: function () {return this. name} Foo. otherscopetest () //->" the foo object! "
<Br/> window. name = "The Window object" </P> <p> function scopetest () {<br/> alert (this. name) <br/>}</P> <p> // calling the function in global scope: <br/> scopetest () <br/> //-> "The Window object" </P> <p> var Foo = {<br/> name: "The Foo object! ", <Br/> otherscopetest: function () {alert (this. name) }< br/>}</P> <p> Foo. otherscopetest () <br/> //-> "the foo object! "<Br/>
RunCode
Based on the principle that native objects are not extended, this bind function (Dom is the scope) is used, which is similar to the BIND of prototype framework.
Dom. bind = function (FN, context) {// if you like the second parameter, you can also change it to thisobject, scope, // In short, it is a new scope object if (arguments. length <2 & context = undefined) return FN; VaR method = FN, slice = array. prototype. slice, argS = slice. call (arguments, 2); return function () {// input the original FN parameter var array = slice. call (arguments, 0); method. apply (context, argS. concat (array ))}
Usage: the first parameter is the function to bind the scope, and the second parameter is window or various objects. Other parameters are random.
<Br/> Dom ={}; <br/> Dom. bind = function (FN, context) {<br/> If (arguments. length <2 & context = undefined) return FN; <br/> VaR method = FN, <br/> slice = array. prototype. slice, <br/> ARGs = slice. call (arguments, 2); <br/> return function () {// input the original FN parameter <br/> var array = slice. call (arguments, 0); <br/> method. apply (context, argS. concat (array) <br/>}</P> <p> window. name = 'this is window'; <br/> var JJ = {<br/> name: 'This is JJ', <br/> alertname: function () {// binding invalid <br/> alert (this. name); <br/>}< br/>}; <br/> var KK ={< br/> Name: "KK zookeeper" <br/>}< br/> function run (f) {<br/> F (); <br/>}</P> <p> Run (JJ. alertname); <br/> var fx2 = Dom. BIND (JJ. alertname, JJ) <br/> Run (fx2); <br/> var FX3 = Dom. BIND (JJ. alertname, window) <br/> Run (FX3); <br/> var fx4 = Dom. BIND (JJ. alertname, KK) <br/> Run (fx4); </P> <p>
Run code
Another example:
<br/> Dom ={}; <br/> Dom. bind = function (FN, context) {<br/> If (arguments. length <2 & context = undefined) return FN; <br/> VaR method = FN, <br/> slice = array. prototype. slice, <br/> ARGs = slice. call (arguments, 2); <br/> return function () {// input the original FN parameter <br/> var array = slice. call (arguments, 0); <br/> method. apply (context, argS. concat (array) <br/>}</P> <p> var OBJ ={< BR/> name: 'A Nice demo ', <br/> FX: function () {<br/> alert (this. name + '\ n' + array. prototype. slice. call (arguments, 0 ). join (','); <br/>}< br/>}; </P> <p> var fx2 = Dom. BIND (obj. FX, OBJ, 1, 2, 3); <br/> fx2 (4, 5); // alerts the proper name, then "1, 2, 3, 4, 5 "<br/>
run the Code