0

I have some code which is giving me the following error when I rename a class name.

This gives no error:

this.container = document.createElement("ul"),

But when I rename the container class name to this:

this.tt-container = document.createElement("ul"),

I get the following error:

Syntax error: Invalid left-hand side in assignment expression

How can I fix this?

  • If `-` were valid for a variable name, then how would you resolve the expression `x-y` - is it subtraction or is retrieving a value from a single variable? And if you say "well, no spaces, therefore it's a variable", then what about `x - y` - isn't this now three variables separated by a space? – VLAZ Sep 23 '18 at 08:31
  • "*when I rename a class name*" - what does your code have to do with class names? – Bergi Sep 23 '18 at 08:33

2 Answers2

1

use _ instead of -, so this.tt_container, not this.tt-container

Lazar Nikolic
  • 4,261
  • 1
  • 22
  • 46
0

From MDN,

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation.

In your case - is not a valid JavaScript identifier. To use -, Use Bracket Notation

this["tt-container"] = document.createElement("ul"),

Rayon
  • 36,219
  • 4
  • 49
  • 76