Declaring multiple variables in JavaScript

后端 未结 17 933
时光说笑
时光说笑 2020-11-22 13:16

In JavaScript, it is possible to declare multiple variables like this:

var variable1 = "Hello, World!";
var variable2 = "Testing...";
var          


        
17条回答
  •  日久生厌
    2020-11-22 13:29

    The concept of "cohesion over coupling" can be applied more generally than just objects/modules/functions. It can also serve in this situation:

    The second example the OP suggested has coupled all the variables into the same statement, which makes it impossible to take one of the lines and move it somewhere else without breaking stuff (high coupling). The first example he gave makes the variable assignments independent of each other (low coupling).

    From Coupling:

    Low coupling is often a sign of a well-structured computer system and a good design, and when combined with high cohesion, supports the general goals of high readability and maintainability.

    So choose the first one.

提交回复
热议问题