所以这是我的代码,你可以看到你有一个名为“Obj”的对象,然后你有一个名为“myClass”的类,它创建的对象与“Obj”对象相同,但对象“newObj”中的run方法是从创建的myClass 输出不同的结果,这是没有意义的,因为两个对象 100% 相同
var x = 10; //global var
var Obj = {
x:30 ,
run : () => {
console.log(this.x);
}
}
console.log(Obj)
Obj.run() //outputs 10
//a class which creates the same 'Obj' Object
class myClass {
constructor() {
this.x=30
this.run = () => {
console.log(this.x)
}
}
}
var newObj = new myClass()
console.log(newObj); //outputs an object which is as identical as the 'Obj' Object
newObj.run(); //outputs 30 instead of 10
就是这样,提前谢谢
慕桂英3389331
哈士奇WWW
相关分类