简体   繁体   English

为什么我不能在JavaScript / nodeJS中使用“名称”作为静态属性

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

I couldn't figure out this, but maybe anyone can help me: When I define a javascript class and try to add a static attribute "name", I can't use "name" afterwards. 我无法弄清楚,但也许任何人都可以帮助我:当我定义一个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);

I would expect it to output all 4 strings. 我希望它能输出所有4个字符串。 But instead it will only output: 但是,它只会输出:

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

This works fine!

It doesn't accept the static attribute "name", but why? 它不接受静态属性“名称”,但是为什么呢? Any ideas/hints? 有什么想法/提示吗? Thanks in advance! 提前致谢!

函数对象的name属性是只读的 ,对其的赋值将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM