Patch
Patch

Reputation: 2507

Pass object to javascript function

I have recently been messing around with jQuery on my website, and I have a fairly limited knowledge of Javascript. I am beginning to like the jQuery ability to pass variables to a jQuery function inside the curly braces, like so:

$(somediv).animate({thisisone: 1, thisistwo: 2}, thisisavar);

What I was wondering is how I can write a Javascript function that I can pass items to inside the curly braces? I know you can write functions like this:

function someName(var1, var2, var3...) {

}

but that doesn't support the braces? I also know that you can add no arguments and do this:

function accident() {
    for( var i = 0; i < arguments.length; i++ ) {
        alert("This accident was caused by " + arguments[i]);
    }
}
accident("me","a car","alcohol","a tree that had no right to be in the path of my driving");

but I also want to pass outside variables instead of just a whole line of strings, if that makes sense?

Basically, I want a function that I can pass variables to, like so:

function myFunction(neededcodehere){
    //Some code here...
}

myFunction (var1, {"Option 1", "Option 2", "Option 3"}, anothervar);

Upvotes: 76

Views: 342130

Answers (6)

ofir_aghai
ofir_aghai

Reputation: 3281

in this way:

  • no need to change your function signature and your function calls.
  • support defaults overrides.
  • use ES6 object destrrcturing.
function yourFunction(args) {
    let defaults = {opt1: true, opt2: 'something'};
    let params = {...defaults, ...args}; // right-most object overwrites 
    const {opt1, opt2, opt3} = params
    console.log(opt1);
}

Upvotes: 1

Philippe Perret
Philippe Perret

Reputation: 78

My cents.

function readable(args) {
    const {opt1, opt2, opt3} = args

 
    console.log(opt1 + opt2 + opt3)
    // => "My favorite things"
}


readable({
      opt1: "My f"
    , opt2: "avori"
    , opt3: "te things"
})

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816232

The "braces" are making an object literal, i.e. they create an object. It is one argument.

Example:

function someFunc(arg) {
    alert(arg.foo);
    alert(arg.bar);
}

someFunc({foo: "This", bar: "works!"});

the object can be created beforehand as well:

var someObject = {
    foo: "This", 
    bar: "works!"
};

someFunc(someObject);

I recommend to read the MDN JavaScript Guide - Working with Objects.

Upvotes: 137

Manuel Guzman
Manuel Guzman

Reputation: 152

Answering normajeans' question about setting default value. Create a defaults object with same properties and merge with the arguments object

If using ES6:

    function yourFunction(args){
        let defaults = {opt1: true, opt2: 'something'};
        let params = {...defaults, ...args}; // right-most object overwrites 
        console.log(params.opt1);
    }

Older Browsers using Object.assign(target, source):

    function yourFunction(args){
        var defaults = {opt1: true, opt2: 'something'};
        var params = Object.assign(defaults, args) // args overwrites as it is source
        console.log(params.opt1);
    }

Upvotes: 7

kqvanity
kqvanity

Reputation: 100

when you pass an object within curly braces as an argument to a function with one parameter , you're assigning this object to a variable which is the parameter in this case

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

function myFunction(arg) {
    alert(arg.var1 + ' ' + arg.var2 + ' ' + arg.var3);
}

myFunction ({ var1: "Option 1", var2: "Option 2", var3: "Option 3" });

Upvotes: 10

Related Questions