5

In some projects that I work, was added some blocks with this syntax:

var [code, name] = input.split("/");
console.log(code);
console.log(name);

I really like this, is so simple and small. It's true I can do this using other syntax like:

var code_name = input.split("/");
console.log(code_name[0]);
console.log(code_name[1]);

But, some browsers like Google Chrome and Opera dont support this "feature". Somebody know if this is a future feature of JS or is deprecated? In case deprecated, exist some beaultiful alternative to the first case?

Thanks for all.

asked Mar 24, 2012 at 14:44
1

3 Answers 3

2

See:

Possible to assign to multiple variables from an array?

for a similar discussion - unfortunately no cross browser solution seems to exist at the moment - safer to use a temp variable

answered Mar 24, 2012 at 14:51
Sign up to request clarification or add additional context in comments.

Comments

2

It's not as nice as the code you proposed, but I think it looks pretty good nevertheless, although you do have to make some workarounds:

First of all, the idea is to make a clean syntax for calling this function, such as:

[5,4,3,2,1].list(a,b,c,d,e);

The idea is that a,b,c,d,e are modified by reference.

Since variables are passed as value and only objects are passed by reference, the variables a,b,c,d,e must be objects.

var a=Object(),b=Object(),c=Object(),d=Object(),e=Object();

Once they are objects, you are able to modify their value by reference (as explained here http://sirdarckcat.blogspot.com.ar/2007/07/passing-reference-to-javascript.html)

(function(val){if(val)this.valueOf=this.toSource=this.toString=function(){return val};return val;}).call(myVariable,'MyNewValue')

So, wrapping it up in a "list" function which extends the Array.prototype, I ended up with this:

 Array.prototype.list = function(){
 for(var i=0;i<this.length;i++){
 (function(val){if(val)this.valueOf=this.toSource=this.toString=function(){return val};return val;}).call(arguments[i],this[i])
 }
 }

So when you call [5,4,3,2,1].list(a,b,c,d,e); you are actually modifying objects by reference (and the result will be an object too).

Here's the full working code:

<!DOCTYPE html>
<html>
 <head><title>test</title>
 <script type="text/javascript">
 Array.prototype.list = function(){
 for(var i=0;i<this.length;i++){
 (function(val){if(val)this.valueOf=this.toSource=this.toString=function(){return val};return val;}).call(arguments[i],this[i])
 }
 }
 </script>
 </head>
 <body>
 <a id="test" href="#">test</a>
 <script type="text/javascript">
 var a=Object(),b=Object(),c=Object(),d=Object(),e=Object();
 [5,4,3,2,1].list(a,b,c,d,e);
 console.log(a,b,c,d,e);
 </script>
 </body>
</html>
halfer
20.2k20 gold badges111 silver badges208 bronze badges
answered Mar 24, 2012 at 16:00

1 Comment

+1 for trying to solve the problem. Note, though: you could just initialize as var a={}.
1

It's a likely future feature of ECMAScript, and currently available in Firefox.

http://wiki.ecmascript.org/doku.php?id=harmony:destructuring

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.