addEventListener
function Person() {
this.init()
}
Person.prototype = {
constructor: Person,
init: function() {
document.documentElement.addEventListener('click', this, false)
},
handleEvent: function(event) {
this.say()
},
say: function() {
alert('hello world!')
}
}
var person = new Person()
Function.prototype.bind
function Person() {
this.init()
}
Person.prototype = {
constructor: Person,
init: function() {
document.documentElement.addEventListener('click', this.handleEvent.bind(this), false)
},
handleEvent: function(event) {
console.log(event)
console.log(this)
}
}
var person = new Person()
Function.prototype.bind Polyfill
function bind(fn, context) {
return function() {
return fn.apply(context, arguments)
}
}