簡體   English   中英

為什么我不能在JavaScript / nodeJS中使用“名稱”作為靜態屬性

[英]Why can't I use “name” as static attribute in javascript / nodeJS

我無法弄清楚,但也許任何人都可以幫助我:當我定義一個javascript類並嘗試添加靜態屬性“名稱”時,此后就不能使用“名稱”。

var MyClass = function() {
  this.name = 'This is an instance of MyClass';
  this.anyName = 'This has nothing to do with it';
}

// static attributes
MyClass.name = 'Why is this not possible?';
MyClass.anyName = 'This works fine!';

var a = new MyClass();
console.log(a.name);
console.log(a.anyName);
console.log(MyClass.name);
console.log(MyClass.anyName);

我希望它能輸出所有4個字符串。 但是,它只會輸出:

This is an instance of MyClass
This has nothing to do with it

This works fine!

它不接受靜態屬性“名稱”,但是為什么呢? 有什么想法/提示嗎? 提前致謝!

函數對象的name屬性是只讀的 ,對其的賦值將被忽略。

暫無
暫無

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

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