In JavaScript, is chained assignment okay?

前端 未结 6 1142
南笙
南笙 2020-12-01 08:59

Am not new to JS or its syntax, but sometimes, the semantics of the language has me stumped at times. At work today, a colleague mentioned this:

var a = b =          


        
6条回答
  •  情深已故
    2020-12-01 09:41

    Your colleague is right. The first statement creates a new, empty array. Then, a reference to this array is assigned to b. Then, the same reference (which is the result of the assignment expression) is assigned to a. So a and b refer to the same array.

    In all other cases, you create two individual arrays.

    By the way: This behavior is quite common and is the same in all C based programming languages. So this is not JavaScript specific.

提交回复
热议问题