5.1.3 对Function扩展
优质
小牛编辑
133浏览
2023-12-01
Method | Kind | Arguments | Description |
bind(object) | instance | object: the object that owns the method | 返回function的实例,这个实例和源function的结构一样,但是它已被绑定给了参数中提供的object,就是说,function中的this指针指向参数object。 |
bindAsEventListener(object) | instance | object: the object that owns the method | 用法和上面的bind一样,区别在于用来绑定事件。 |
让我们看看如何运用这些扩展。
<input type=checkbox id=myChk value=1> Test? <script> //declaring the class var CheckboxWatcher = Class.create(); //defining the rest of the class implementation CheckboxWatcher.prototype = { initialize: function(chkBox, message) { this.chkBox = $(chkBox); this.message = message; //assigning our method to the event this.chkBox.onclick = this.showMessage.bindAsEventListener(this); }, showMessage: function(evt) { alert(this.message + ' (' + evt.type + ')'); } }; var watcher = new CheckboxWatcher('myChk', 'Changed'); </script>