次のコードでは、
class PersonClass {
constructor(fname) {
this.fname = fname;
}
read = function() { console.log('I am reading') }
speak () { console.log('I am speaking'); }
}
//Instantiate
let p1 = new PersonClass('Raj')
read = function() { console.log('I am reading') }
新しく作成されたインスタンスのプロパティになります。
p1.hasOwnProperty('read')
はtrue
speak() { console.log('I am speaking'); }
に割り当てられるのとは対照的にPersonClass.prototype
。すなわち
p1.hasOwnProperty('speak')
はFalse
p1.__proto__.hasOwnProperty('speak')
はtrue
誰かがなぜこれが起こるのか説明できますか.
基本的に、クラス内のメソッド宣言の 2 つの方法の違いは何ですか。
(ES6で)speak() {...}
の短い構文だと思いましたspeak = function() {...}
ありがとう