javascript bind绑定函数代码
2010-01-05 00:00
656 查看
具体结论可参见《javascript下动态this与动态绑定实例代码》。本文专注设计一个无侵入的绑定函数。
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!"
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
基于不扩展原生对象的原则,弄了这个bind函数(dom为作用域),用法与Prototype框架的bind差不多。
用法:第一个参数为需要绑定作用域的函数,第二个为window或各种对象,其他参数随意。
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);
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
另一个例子:
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"
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
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!"
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
基于不扩展原生对象的原则,弄了这个bind函数(dom为作用域),用法与Prototype框架的bind差不多。
dom.bind = function(fn,context){ //第二个参数如果你喜欢的话,也可以改为thisObject,scope, //总之,是一个新的作用域对象 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或各种对象,其他参数随意。
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);
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
另一个例子:
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"
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
相关文章推荐
- javascript bind绑定函数代码
- Javascript学习---函数绑定bind()
- Javascript-bind绑定函数
- JavaScript笔记2--Function.prototype.bind动态绑定函数
- JavaScript-bind()与函数绑定
- javascript使用bind()函数绑定this
- JavaScript格式化数字的函数代码
- javascript trim 去空格函数实现代码
- jQuery动态添加的元素绑定事件处理函数代码
- JavaScript学习代码整理(二)--函数
- JavaScript 模仿vbs中的 DateAdd() 函数的代码
- Javascript操作cookie的函数代码
- JavaScript中使用bind()方法让代码更干净
- javascript Array数组对象的扩展函数代码
- JavaScript实现为事件句柄绑定监听函数的方法分析
- javascript一些不错的函数脚本代码
- C++拾遗--bind函数绑定
- 五十行javascript代码实现简单的双向数据绑定
- jquery快捷动态绑定键盘事件的操作函数代码
- jquery中bind函数绑定多个事件