0
function go(a, b){
        console.log(a);
        console.log(b);
    }

go(b="happy", a="sad");

How can I make this work, just like it does in python?

3 Answers 3

3

Not really part of the language, but you can fake it like this: http://www.javascriptkit.com/javatutors/namedfunction.shtml

Example:

function go( params ) {
    console.log(params.a);
    console.log(params.b);
}

go( { b:"happy", a:"sad"} );
Sign up to request clarification or add additional context in comments.

Comments

0
go(sad, happy);

Just be sure you use the same order. If the function is declared as:

function(a,b){}

Then the attributes must be a,b; not b,a.

Alternatively, if you really want to name them, use a colon in front of the attribute name.

Comments

0

While there are some libraries which claim that they will let you do that (Prototype has a backwards way of making this possible), it is not cross browser compliant and it certainly isn't the the ECMAScript specification that JS is based off of.

The only way to have reliable behavior is to use the arguments in the order they were listed.

Comments

Your Answer

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.