In JavaScript, is chained assignment okay?

前端 未结 6 1158
南笙
南笙 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:26

    Your colleague is right:

    var a = b = [];
    a.push('something');
    console.log(b);          // outputs ["something"]
    

    but:

    var a = [], b = [];
    a.push('something');
    console.log(b);          // outputs []
    

提交回复
热议问题