skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search