Angular2 - 为什么不能在 属性 名称中使用破折号或其他特殊字符

Angular2 - why cant use dash or other special characters in property name

为什么我不能在这里使用破折号?

export class FizzService {
  getFizzes() : object[] {
    return[
      {
        iconClass: 'glyphicon glyphicon-search',
      },

所以在上面我不能使用

icon-class: 'glyphicon glyphicon-search',

我必须做的:

iconClass: 'glyphicon glyphicon-search',

否则我会看到错误:

属性 名称可以是字符串 ("foo") 或标识符 (foo)。

标识符也可以用于变量。

标识符不能包含连字符,因为那是 minus 运算符。

amount-discount 表示 "Amount minus discount" 而不是 "A variable with a hyphen in the name".

标识符也不能包含空格、+ 个字符和大量其他字符。如果您希望 属性 名称包含特殊字符,请使用字符串。

这是因为Object的属性不能包含除下划线以外的任何特殊字符(例如:property_name)。

如果您想在对象的 属性 中使用连字符,那么我们应该将 属性 放在引号中(例如:"property-name")。

请阅读下面的 link 以了解 javascript 中的命名约定。 http://javascript.crockford.com/code.html

只需将带有特殊字符的 属性 名称用引号括起来,即:

'ng-class':'glyphicon glyphicon-house'