簡體   English   中英

是否可以在 js class 關鍵字中聲明原型屬性

[英]Is it possible to declare prototype properties within the js class keyword

class A {
   // Will be declared on the class instance
   name = “one”;

   // Will be declared on the class constructor 
   static otherName = “two”;

   // Will be on the prototype
   // Is this also possible for properties? (not just for methods)
   greet() {}
}

是否可以使用此語法在 class 的原型上聲明屬性?

謝謝。

不,這不是任何領域提案的一部分。 您需要使用Object.defineProperty單獨創建它們。

Object.defineProperty(A.prototype, 'name', {
   value: 'one',
   configurable: true,
});

您可以定義一個getter

 class A { get x(){ return 1; } } console.log(A.prototype.x);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM