这是示例代码。
class TestClass {
constructor() {
let question = "How could I refer this variable inside of the nested object?"
}
}
TestClass.prototype.category = {
validate : function () {
return true;
}
, read : function () {
// this in here is not TestClass. How could I refer TestClass Instance(caller) in strict mode ?
console.log('Inside of TestClass.prototype.category.read', this);
if(this.validate) {
console.log('I would like to refer TestClass.question', this.question);
}
}
}
TestClass.prototype.readCategory = function () {
this.category.read();
}
然后,我在chrome编辑器控制台中执行以下操作。
var test = new TestClass();
test.readCategory();
// result is like below
I would like to refer TestClass.question undefined
据我所知,我想像
new
关键字时,我将生成一个包含question
变量和推入prototype
的方法的实例readCategory()
,并调用instance.category.read
,但此时,this
关键字将指向instance.read
对象,而不是TestClass
实例。因此this.question
将是一个undefined
值。我发现当我们使用class
时,无法使用this.caller
。(自动应用严格模式)。
在这种情况下如何访问类变量?谢谢。
答案 0 :(得分:0)
不能,您的category
变量是构造函数完全私有的。构造函数返回后,它就消失了。
您的结构是相当不寻常的,但是如果您确实需要category
作为从属对象,但是仍然希望它可以访问它的一部分TestClass
实例,则可以使用创建的箭头函数在构造函数中实现该目标,请参见注释:
class TestClass {
constructor() {
// Make question a property
this.question = "How could I refer this variable inside of the nested object?"
// Make category an instance property using arrow functions
this.category = {
validate : () => {
return true;
}
, read : () => {
console.log('Inside of TestClass.prototype.category.read', this);
if(this.category.validate()) { // <=== Need to add `category.` and `()`
console.log('I would like to refer TestClass.question', this.question);
}
}
};
}
// No reason to define this outside the `class` construct, make it a method
readCategory() {
this.category.read();
}
}
使用class fields proposal(当前处于阶段3,因此您需要进行转译),也可以这样编写:
class TestClass {
// Make category an instance property using arrow functions
category = {
validate : () => {
return true;
}
, read : () => {
console.log('Inside of TestClass.prototype.category.read', this);
if(this.category.validate()) { // <=== Need to add `category.` and `()`
console.log('I would like to refer TestClass.question', this.question);
}
}
};
constructor() {
// Make question a property
this.question = "How could I refer this variable inside of the nested object?"
}
// No reason to define this outside the `class` construct, make it a method
readCategory() {
this.category.read();
}
}
这实际上与第一个示例相同;字段初始化程序就像在构造函数中一样执行。
如果您不希望question
成为实例的属性,因为这些是构造函数中定义的箭头函数,则可以将其保留为它们封闭的局部变量:
class TestClass {
constructor() {
let question = "How could I refer this variable inside of the nested object?"
this.category = {
validate : () => {
return true;
}
, read : () => {
console.log('Inside of TestClass.prototype.category.read', this);
if(this.category.validate()) {
console.log('I would like to refer TestClass.question', question); // <== No `this.`
}
}
};
}
readCategory() {
this.category.read();
}
}