【发布时间】:2017-09-26 15:59:35
【问题描述】:
您好,我正在尝试运行以下程序。我有一个函数hello,我在b 内部调用它。它给了我一个错误
TypeError: 无法读取未定义的属性 'hello'
class ChildClass {
// constructor
constructor(param, ...args) {
this.hello = function (a) {
console.log(a);
}
this.obj = {
a: {
b() {
this.hello(2)
}
}
}
}
}
有谁知道我在这里做错了什么?
【问题讨论】:
-
this在b()函数中将指向this.a对象 -
b方法中的this不是指ChildClass的对象 -
@AndreiCACIO 是的。我怎样才能使
hello在 b() 中可用
标签: javascript