prototype.js学习(3)函数绑定
2008-02-22 10:25
417 查看
类库提供了Function.prototype.bind Function.prototype.bindAsEventListener两个方法。首先我们从概念上解释一个这两个方法。
任何一个函数都可以调用这两个方法;参数的是javascript对象或网页上元素对象;返回类型是个函数对象。
本来我就是个函数,返回还是函数,到这两个函数有什么不同呢。看实现代码,关键还是apply/call函数的代码。其实这里只是转化了一下方法调用的对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input type="checkbox" id="myChk" name="asf" value="1" />Test
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript" type="text/javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//函数绑定:bindAsEventListener()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var CheckboxWatcher = Class.create();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
CheckboxWatcher.prototype = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
initialize: function(chkBox, message) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.chkBox = $(chkBox);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.message = message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//绑定点击onclick事件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.chkBox.onclick = this.showMessage.bindAsEventListener(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
showMessage: function(evt) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(this.message + ' (' + evt.type + ')');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new CheckboxWatcher('myChk','message!!!!');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//$('myChk').onclick=function(){}; //正常情况下的事件写法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<br />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input type="checkbox" id="myChk2" name="asf" value="1" />Test2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript" type="text/javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//函数绑定:bind()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//obj.getName.bind($('myChk2'));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//相当于从apply/call,应用某一对象的一个方法,用另一个对象替换当前对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//即:应用obj的getName方法,用$('myChk2')替换当前obj对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Class()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name="class";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Class.prototype.getName=function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(this.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj=new Class();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//$('myChk2').onclick=obj.getName; //输出asf
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//$('myChk2').onclick=obj.getName.bind(obj); //输出: class;或用bindAsEventListener也可以
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
$('myChk2').onclick=obj.getName.bind($('myChk2')); //输出:asf
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//从上面代码可以看出bind/bindAsEventListener只是包装了一下apply/call方法,改变方法的调用对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如例子,你可以把obj.getName方法转化成任何对象调用,并且把方法让表单元素触发。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//(bind和bindAsEventListener之间只是返回函数的参数不同)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript" type="text/javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Class1(name)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Class1.prototype.getName=function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(this.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Class2(name)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.getName=Class1.prototype.getName.bind(this); //这句话
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj1=new Class2("yql");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj1.getName(); //输出yql
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj2=new Object();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.name="zkj";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.fun=Class1.prototype.getName.bind(obj2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.fun();//输出zkj
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
任何一个函数都可以调用这两个方法;参数的是javascript对象或网页上元素对象;返回类型是个函数对象。
本来我就是个函数,返回还是函数,到这两个函数有什么不同呢。看实现代码,关键还是apply/call函数的代码。其实这里只是转化了一下方法调用的对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input type="checkbox" id="myChk" name="asf" value="1" />Test
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript" type="text/javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//函数绑定:bindAsEventListener()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var CheckboxWatcher = Class.create();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
CheckboxWatcher.prototype = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
initialize: function(chkBox, message) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.chkBox = $(chkBox);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.message = message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//绑定点击onclick事件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.chkBox.onclick = this.showMessage.bindAsEventListener(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
showMessage: function(evt) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(this.message + ' (' + evt.type + ')');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new CheckboxWatcher('myChk','message!!!!');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//$('myChk').onclick=function(){}; //正常情况下的事件写法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<br />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input type="checkbox" id="myChk2" name="asf" value="1" />Test2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript" type="text/javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//函数绑定:bind()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//obj.getName.bind($('myChk2'));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//相当于从apply/call,应用某一对象的一个方法,用另一个对象替换当前对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//即:应用obj的getName方法,用$('myChk2')替换当前obj对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Class()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name="class";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Class.prototype.getName=function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(this.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj=new Class();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//$('myChk2').onclick=obj.getName; //输出asf
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//$('myChk2').onclick=obj.getName.bind(obj); //输出: class;或用bindAsEventListener也可以
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
$('myChk2').onclick=obj.getName.bind($('myChk2')); //输出:asf
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//从上面代码可以看出bind/bindAsEventListener只是包装了一下apply/call方法,改变方法的调用对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如例子,你可以把obj.getName方法转化成任何对象调用,并且把方法让表单元素触发。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//(bind和bindAsEventListener之间只是返回函数的参数不同)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript" type="text/javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Class1(name)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Class1.prototype.getName=function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(this.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Class2(name)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.getName=Class1.prototype.getName.bind(this); //这句话
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj1=new Class2("yql");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj1.getName(); //输出yql
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj2=new Object();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.name="zkj";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.fun=Class1.prototype.getName.bind(obj2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.fun();//输出zkj
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- js函数prototype属性学习(二)
- 【js学习之路】事件绑定函数中,将this换成调用对象导致的问题
- prototype.js日常应用函数积累
- django学习(二)之分页、vue.js数据绑定及模态对话框的使用
- Knockout.Js官网学习(options绑定)
- Knockout.Js官网学习(selectedOptions绑定、uniqueName 绑定)
- prototype.js的系列文章——$F()函数
- 【js学习笔记-047】-- 函数的实参和形参
- 从两个函数来学习js闭包的概念
- js循环动态绑定带参数函数遇到的问题及解决方案[转]
- Ajax学习笔记之Prototype的工具函数汇总
- js学习笔记之函数
- js面向对象学习笔记之七(函数 与 基础控制结构)
- prototype.js常用函数及其用法
- Vue.js学习笔记:v-model双向绑定
- js学习笔记:函数
- prototype使用学习手册指南之Position.js
- js中函数对象的属性,函数原型属性length、name、prototype
- v8学习---添加js全局函数
- JS 事件基础 事件绑定函数