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 dynamic this and dynamic binding instance code in javascript. This article focuses on designing a non-intrusive binding function.
Script 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! "Script"
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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.
The Code is as follows:
Dom. bind = function (fn, context ){
// If you like the second parameter, you can 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 here
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.
Script dom ={}; dom. bind = function (fn, context) {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)} window. name = 'this is window'; var jj = {name: 'This is jj', alertName: function () {// The binding fails. alert (this. name) ;}}; var kk = {name: "kk zookeeper"} function run (f) {f ();} run (jj. alertName); var fx2 = dom. bind (jj. alertName, jj) run (fx2); var fx3 = dom. bind (jj. alertName, window) run (fx3); var fx4 = dom. bind (jj. alertName, kk) run (fx4); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Another example:
Script dom ={}; dom. bind = function (fn, context) {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)} var obj = {name: 'A nice demo', fx: function () {alert (this. name + '\ n' + Array. prototype. slice. call (arguments, 0 ). join (',') ;}}; var fx2 = dom. bind (obj. fx, obj, 1, 2, 3); fx2 (4, 5); // Alerts the proper name, then "1, 2, 3, 4, 5" script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]