【问题标题】:Why can't I use "name" as static attribute in javascript / nodeJS为什么我不能在 javascript / nodeJS 中使用“名称”作为静态属性
【发布时间】:2013-03-03 12:39:39
【问题描述】:

我无法弄清楚这一点,但也许有人可以帮助我: 当我定义一个javascript类并尝试添加一个静态属性“name”时,之后我不能使用“name”。

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”,但为什么呢?任何想法/提示? 提前致谢!

【问题讨论】:

标签: javascript node.js naming static-variables


【解决方案1】:

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

【讨论】:

  • 我得说,这很蹩脚。即使您“使用严格”;它仍然默默地失败。
猜你喜欢
  • 2015-01-12
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
相关资源
最近更新 更多