Is the expression in brackets in Javascript a link reference?

I understand that the expression in parentheses in Javascript returns the result of evaluating the expression in parentheses:

x = ( 1, 2, 3 ); 

will evaluate the three above expressions and return the result of the latter: '3', as mentioned in some other posts.

The following code sample from SlickGrid contains what I'm not quite sure I understand:

  $(function () { for (var i = 0; i < 500; i++) { var d = (data[i] = {}); d["title"] = "Record " + i; d["n1"] = Math.round(Math.random() * 10); d["n2"] = Math.round(Math.random() * 10); d["n3"] = Math.round(Math.random() * 10); d["n4"] = Math.round(Math.random() * 10); d["n5"] = Math.round(Math.random() * 10); } grid = new Slick.Grid("#myGrid", data, columns, options); }) 

In particular, the expression:

 var d = (data[i] = {}); 

seems to return a reference to the associative array initialized in the expression in parentheses.

Is this really what is going on? Is there a more detailed explanation for this? Is there a reason to do this instead of something more obvious, such as creating an associative array 'd' and then setting it to 'data [i]'?

+4
source share
5 answers

Yes. Using the line:

 var d = (data[i] = {}); 

The variable d and data[i] will eventually refer to the same empty object created by {} .

In JavaScript, the = operator sets the variable on the left and returns the given value. This means that you can do the assignment in the middle of another expression, possibly with parentheses, to ensure the correct order of work.

In your example, brackets are optional - the following will have the same result, because associativity = is from right to left:

 var d = data[i] = {}; 

Another arbitrary example is the assignment of a function call in an argument:

 alert(d = "hello"); 

Or when assigning a reference to an object, you can use the result to work with the object:

 var d; (d = {}).prop1 = "test"; console.log(d.prop1); // prints "test" 
+6
source

It is just a short hand.
Your code

 var d = (data[i] = {}) 

Is equal

  data[i] = {}; var d = data[i]; 
+2
source

This makes the code shorter, but harder to read. This is an ordinary hack. People usually try to avoid variables that will be used only once; especially if they can contain a string under 80 characters.

 var d = (data[i] = {}); 

equivalently

 var d = {}; data[i] = d; 

In other languages, you will usually see something like this:

 function(str) { if (trimmed=str.trim()) { console.log(trimmed); } } 
0
source

Yes. Try the following code

 var theObject={a:"hhelo",b:"no"} hello={{a:"blah"},theObject} console.log(hello) //Object {a: "hhelo", b: "no"} hello.a="b"; console.log(theObject) //Object {a: "b", b: "no"} 

So yes, it returns a link

0
source

You are right about the reference (nothing special in parentheses, just the assignment operator returns what was assigned last, so you can set multiple values ​​in the same thing: x=y=3; ), but I can't come up with a good one reason to write code this way. A little reordering of tasks, and you essentially have the same thing, and trivially split into two lines:

var d = {},data[i] = d;

0
source

Source: https://habr.com/ru/post/1495687/


All Articles