您需要使用bracket notation从this
上下文中获取属性值。
for(property in this) {
console.log(property + "=" + this[property]);
// ----^^^^^^^^^^^^^^----
}
可以使用Object.keys
方法,它返回属性名称的数组获取对象的属性。
FYI:要访问this
范围内的另一种情况下是指其使用具有两个上下文范围内的变量(通常可以使用全局变量)。
// initial variable or neglect to make it as global
var self;
/* cotext 1 start */
// define
self = this;
for(property in self) {
console.log(property + "=" + self[property]);
}
/* cotext 1 end */
/* cotext 2 start */
// use `self` to refer the context1
for(property in self) {
console.log(property + "=" + self[property]);
}
/* cotext 2 end */
对象没有名称? – Liam
请发布您的完整代码@Faizy –
@Liam他们做,如果他们使用构造函数或新的ES6类语法创建。 – WilomGfx