Home  >  Article  >  Web Front-end  >  javascript bind binding function code_javascript skills

javascript bind binding function code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:37:21832browse

For specific conclusions, please refer to "Dynamic this and dynamic binding example code under javascript". This article focuses on designing a non-intrusive binding function.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Based on not extending native objects In principle, I created this bind function (dom is the scope), and its usage is similar to the bind of the Prototype framework. The code is as follows:


dom.bind = function(fn,context){
//If you like, the second parameter can also be changed 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(){//here Pass in the parameters of the original fn
var array = slice.call(arguments, 0);
method.apply(context,args.concat(array))
}


Usage: The first parameter is the function that needs to be bound to the scope, the second is the window or various objects, and other parameters are optional.

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
Another example:
[Ctrl A select all Note: <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>If you need to introduce external Js, you need to refresh to execute <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(){//这里传入原fn的参数 var array = slice.call(arguments, 0); method.apply(context,args.concat(array)) } } window.name = 'This is window'; var jj = { name: '这是jj', alertName: function() {//绑定失效 alert(this.name); } }; var kk = { name:"kkです" } 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>]<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(){//这里传入原fn的参数 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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn