Flávio Ricardo
Flávio Ricardo

Reputation: 75

JavaScript alternatives

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.

Upvotes: 5

Views: 1102

Answers (3)

fsodano
fsodano

Reputation: 548

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>

Upvotes: 2

Vijay Agrawal
Vijay Agrawal

Reputation: 3821

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

Upvotes: 2

user1106925
user1106925

Reputation:

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

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

Upvotes: 1

Related Questions