0

Is there a functional difference between these two examples? Why would you prefer one over the other?

Static property:

class FirstExample {
    static baz = "Something";
    static biz = 10;

    //rest of class
}

Adding using class name:

class SecondExample {
     //Rest of class
}

SecondExample.biz = "Something";
SecondExample.baz = 10;
1
  • The first one is more self-contained and less repetitive…?! Commented Mar 10, 2020 at 9:35

2 Answers 2

1

Is there a functional difference between these two example?

No.

Why would you prefer one over the other?

You could consider the first a bit more self-contained and better expressing intent. Since the properties are defined statically, tools and even the JavaScript interpreter have it easier to analyze the class.

The second form is only needed if you need to add properties that are only known at run time (but that might not be a good idea anyway).


If feel like this is a reoccurring question/problem with new JS syntax. A lot of the new syntax is just syntactic sugar, i.e. it doesn't solve a problem that couldn't be solved differently before.

Of course you can keep doing what you did before and just assign the properties like any other property. At the surface the functionality is the same.

But as I hinted at above, the advantage of dedicated syntax is easier static analysis for external tools and the interpreter itself.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I just wanted to make sure because I couldn't think of any reason why this pattern was followed instead of using static. None of these properties are only available at run time, so I can safely refactor to use static keyword and clean up the code a bit.
1

In the given example it wouldn't make any difference, but there are cases where it could, for example if a class method was use in a moment where the variables were yet not defined, since in the second case they are being defined outside.

5 Comments

Can you provide an example? I can't imagine a case where this would be a problem.
` class SecondExample { //Rest of class } console.log(SecondExample.biz); // undefined console.log(SecondExample.baz); // undefined SecondExample.biz = "Something"; SecondExample.baz = 10; console.log(SecondExample.biz); // Something console.log(SecondExample.baz); // 10 `
That's not a class method however... I'm aware that the properties don't exist before they are assigned.
the point is just that there is gonna be a moment in which those variables will not be filled, and in the first case there isn't.
Sure, but I'd hope that people are smart enough to realize when they call functions that need those variables before they are assigned. Put the assignments directly after the class definition and you are good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.