GitHub 博客园 Nanakon

-_-#函数绑定

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)
    }
}

 

posted on 2014-09-02 13:17  jzm17173  阅读(268)  评论(0编辑  收藏  举报

导航

轻音