Search code examples
javascriptangularangular2-services

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


Why I can not use dash here?

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

So in above I can not use

icon-class: 'glyphicon glyphicon-search',

I must do:

iconClass: 'glyphicon glyphicon-search',

Otherwise I see error:

enter image description here


Solution

  • Property names can be strings ("foo") or identifiers (foo).

    Identifiers can also be used for variables.

    Identifiers cannot include a hyphen because that is the minus operator.

    amount-discount means "Amount minus discount" and not "A variable with a hyphen in the name".

    Identifiers can't include spaces, + characters and a host of other characters either. Use a string if you want a property name to include a special character.