javascript - 在位于同一类的对象中的方法内引用类局部变量
问题描述
这是示例代码。
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
。(自动应用严格模式)。
在这种情况下我将如何访问类变量?谢谢。
解决方案
你不能,你的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();
}
}
使用类字段提案(目前处于第 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();
}
}
推荐阅读
- javascript - 具有功能 insertContent 的 tinymce 5 按钮创建不必要的元素
- python - 缩小图像会增加内存消耗?
- sql - 同一结果中有 2 个不同的列
- c# - 串口 DataReceived 事件未触发
- excel - 如何在 excel 中查看 fwd postgres 表?
- node.js - 防止 socket.io-client 在有 onbeforeunload 时断开连接
- python - 如何解决错误:“int”对象不支持项目分配
- javascript - 过滤数据以在点击时加载特定响应
- sql - SQL:无法在 where 中使用列别名
- javascript - 在页面上显示对象数组