問題タブ [class-fields]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
javascript - JS クラス: クラス宣言における ES6 myFunc(){..} と ES5 myFunc = function() {...} の違い
次のコードでは、
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() {...}
ありがとう