首页 > 网站 > WEB开发 > 正文

-_-#函数绑定

2024-04-27 14:19:32
字体:
来源:转载
供稿:网友

-_-#函数绑定

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表