美文网首页
JavaScript(func.bind)

JavaScript(func.bind)

作者: JetLu | 来源:发表于2016-09-13 18:07 被阅读27次

示例代码



/*
* Node.js(6.x)里运行并不能得到预期的结果,浏览器下可行
* 代码只是作简单解释用,真正使用需略作修改
*/
const o = {
    name: 'genius'
}

const echo = function() {
    console.log(this.name, arguments)
}

Function.prototype.bind = function(o, ...params) {
    const that = this
    return function(...args) {
        return that.apply(o, [...params, ...args])
    }
}

o.echo = echo.bind({name: 'talent'}, 6)
o.echo(9)
// talent [6]

参考


相关文章

网友评论

      本文标题:JavaScript(func.bind)

      本文链接:https://www.haomeiwen.com/subject/ixnmettx.html