示例代碼
/*
* Node.js(6.x)里運行并不能得到預期的結(jié)果,瀏覽器下可行
* 代碼只是作簡單解釋用后裸,真正使用需略作修改
*/
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]