何时c1.rand
调用我不清楚解释器如何访问this.num
,因为尚未调用构造函数。它不应该调用constructor()
rand 来设置 的值this.num
吗?
class C {
constructor() {
this.num = Math.random();
}
rand() {
console.log( "Random: " + this.num );
}
}
var c1 = new C();
c1.rand(); // "Random: 0.4324299..." (any number from 0 to 1)