1

Consider:

class Welcome{ static name="something"; // Throws an error } 

MDN says static will be supported from Chrome 42. But it is throwing a syntax error in Chrome 70 and below versions.

7
  • 2
    CanIUse says version 72. Seems the MDN browser data is off. Commented Oct 31, 2023 at 5:41
  • 1
    @VLAZ MDN says 72 as well. It's static method syntax in classes that was introduced with chrome 42 Commented Oct 31, 2023 at 5:59
  • 2
    WTH are you using a Chrome browser that's over 4 years old? Commented Oct 31, 2023 at 5:59
  • 2
    @Bergi Probably an unfortunate web developer having to support legacy browsers? Commented Oct 31, 2023 at 6:09
  • 3
    @InSync I've seen many a legacy environment, but none with legacy Chrome Commented Oct 31, 2023 at 6:16

2 Answers 2

3

MDN says static will be supported from Chrome 42.

MDN Web Docs does not paint a detailed picture here, as this article deals with static class methods, static blocks and static class fields. Support for these different uses of static did not all come in Chrome version 42. The support for static class fields came later. On the MDN Web Docs page dedicated to Public class fields it mentions Chrome version 72:

enter image description here

If a browser doesn't have support for static class fields, you can use this code instead:

function defineStaticField(cls, prop, value) { Object.defineProperty(cls, prop, { value, writable: true, configurable: true, enumerable: true, }); } class Welcome{ } defineStaticField(Welcome, "name", "something"); 
Sign up to request clarification or add additional context in comments.

2 Comments

…or really just Welcome.name = "something" (well, maybe for anything else but name)
That's the thing: their example was with name.
0

To fix this, you can use a transpiler like Babel.

Transpilers convert code written in newer versions of JavaScript into an older, more widely supported syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.