JavaScript construct equivalent to PHP list ()?

Possible duplicate:
Javascript equivalent to PHP list ()

In PHP you can do the following:

list($b,$c,$d) = array("A","B","C"); 

Is there anything similar in JS?

+4
source share
3 answers

People seem to hate the with () construct in javascript, but anyway ...

 function f(){return {a:1, b:2};} with(f()) { alert(a);//1 } // or function combine(propertyNames, values) { var o = {}; for (var i=0; i<propertyNames.length; i++) { o[propertyNames[i]] = values[i]; } return o; } with (combine(['a', 'b'], [1, 2])) { alert(b);//2 } 
0
source

Yes, it is possible since JavaScript 1.7

You can do:

 function f() { return [1, 2]; } [a, b] = f(); 
+2
source

I believe this was introduced in JavaScript 1.7. This means that you cannot use it in most browsers.

 [a,b] = [14,15]; // or [a,b] = [b,a]; // or [a,b] = someFuncThatReturnsArray(); 

See MDN for more details.

0
source

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


All Articles