Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
like image 663
Connor Deckers Avatar asked Oct 14 '11 07:10

Connor Deckers


People also ask

Can you pass an object to a function in JavaScript?

To pass an object to a JavaScript function, we can add a parameter that accepts an object. const someFunc = (arg) => { alert(arg. foo); alert(arg. bar); }; someFunc({ foo: "This", bar: "works!" });

Can you pass an object into a function?

Just as passing objects to functions as arguments is an efficient way of moving information to where it's needed, so is using objects as return values. Functions can either manipulate objects that you pass to them and then return them, or they can return brand-new objects that they create in the function body.

How do you pass an object as a parameter to a function?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.


2 Answers

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.

like image 181
Felix Kling Avatar answered Sep 19 '22 11:09

Felix Kling


function myFunction(arg) {     alert(arg.var1 + ' ' + arg.var2 + ' ' + arg.var3); }  myFunction ({ var1: "Option 1", var2: "Option 2", var3: "Option 3" }); 
like image 40
Darin Dimitrov Avatar answered Sep 19 '22 11:09

Darin Dimitrov