For more information, see dynamic this and dynamic binding instance code in JavaScript. This article focuses on designing a non-intrusive binding function. <textarea id="runcode53521"></textarea>
[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.CopyCodeThe 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.
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
another example:
[Ctrl + A select all Note: To introduce external JS, refresh it before execution]