In javascript, what do multiple equal signs mean? [duplicate]

房东的猫 提交于 2019-12-03 15:22:09

问题


I saw this code somewhere, but what does it mean? (all a, b, c are defined previously)

var a = b = c;

回答1:


It quickly assigns multiple variables to a single value.

In your example, a and b are now equal set to the value of c.

It's also often used for a mass assign of null to clean up.

a = b = c = d = null;



回答2:


  1. Assign c to b.
  2. Assign b to a.

So if I say var a = b = 1;

>>> var a = b = 1;
undefined
>>> a
1
>>> b
1



回答3:


This means a, b and c are the same reference.

For example:

var c = {hello: "world"};
var a = b = c;

// now all three variables are the same object



回答4:


It's a shorthand for:

var a;
var b;
b=c;
a=b;

It's meant as a combination of assigning the same value to two or more other variables, and declaring these variables in local scope at the same time.

You can also use this syntax independently of the var declaration:

var a;
var b;
a=b=c;


来源:https://stackoverflow.com/questions/21215418/in-javascript-what-do-multiple-equal-signs-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!