4

I am trying to create a function with options. I believe I have to use objects but have failed so far. By options I mean something like that:

insertButton({
    settings:{
        value1:'Some text'      
    }
});

function insertButton(settings){
    settings = new Object();    
    document.write(settings.value1);
}

Obviously, that won't work but I am trying to show what I mean. Maybe someone could help.

I am asking because right now I have simple function where I can pass values with strict order. With options I want to be undependable of variables order in function. For example:

function insertButton2(value1,value2,value3){
    document.write(value1);
    document.write(value2);
    document.write(value3);   
}

insertButton2('a','','c');  //empty commas to skip value2 

Leaving empty commas to make sure 'c' is value3 is not convenient for me. That is why I would like to try objects, options.

3 Answers 3

9

You just pass into the function an object with the required keys/values:

function parameterify(params) {
    console.log(params.isAwesome);
}

parameterify({
    isAwesome : true
});
//logs true

You had two mistaknes:

  1. There are no names parameters in js, so {settings:{}} would pass an object with a key of settings (so that inside of the functions, you'd have to do settings.settings)

  2. You redeclared settings at the top of the function (settings = new Object();), which no matter what you pass in will always overwrite it. As a side note, new Object is iffy - object literal {} is way cooler

Sign up to request clarification or add additional context in comments.

4 Comments

@Alex Yep, that simple! Isn't javascript awesome?
It is! I am glad I was on correct path. Thank you for shooting out solution so quick. Thank you all guys, I see all replies more or less of the same work around. @Zirak, a quick question if you dont mind. Is there any common way to conver object to string?
@Alex There's always obj.toString(), but I'll guess that's not what you want to do. For plain ol' objects, you can simply loop over every key and add call toString on it. Of course, it'll have to be recursive, since you'll have to do it for every object you find.
Thank you! I see what you mean.
2

Well you're overwriting settings to a blank Object in the first line of your function, take that out and it will work...

Edit: Sorry early remove the settings object from the arguments should just be

insertButton({
       value1:'Some text'      
});

3 Comments

if i remove the line how you say then it still returns undefined.
You changed your question... hold on.
My second example is just to help to explain what I want to achive. Sorry if that is confusing.
2

Try this

function insertButton(settings){
    document.write(settings.value1);
}

insertButton({
    value1:'Some text'      
});

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.