-_-#함수 바인딩

2614 단어 함수

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)

    }

}

좋은 웹페이지 즐겨찾기