.bind 不适用于 es6 class 实例
.bind not working with es6 class instance
使用 bind
文档,如果我用 es6 class 实例替换对象(在他们的示例中定义为 module
),它不会绑定。
这是文档...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
这是我的代码...
class Foo {}
let foo = new Foo()
let fooVar = 'foo var'
let fooFunc = () => {
return this.var
}
foo['var'] = fooVar
fooFunc.bind(foo)
foo['func'] = fooFunc
// i expected this to return 'foo var', but instead get 'undefined'
foo.func()
我怎样才能将实例方法添加到现有实例,并使其正确绑定?
如果您阅读有关箭头函数的文档,您会看到:
Does not have its own bindings to this or super, and should not be used as methods.
因此你不能绑定一个新的 this
如果它没有
使用 bind
文档,如果我用 es6 class 实例替换对象(在他们的示例中定义为 module
),它不会绑定。
这是文档... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
这是我的代码...
class Foo {}
let foo = new Foo()
let fooVar = 'foo var'
let fooFunc = () => {
return this.var
}
foo['var'] = fooVar
fooFunc.bind(foo)
foo['func'] = fooFunc
// i expected this to return 'foo var', but instead get 'undefined'
foo.func()
我怎样才能将实例方法添加到现有实例,并使其正确绑定?
如果您阅读有关箭头函数的文档,您会看到:
Does not have its own bindings to this or super, and should not be used as methods.
因此你不能绑定一个新的 this
如果它没有