1

Say I have a function like this

function myFunc(a, b, c)
{
    if(a!=undefined) this.a = a;
    if(b!=undefined) this.b = b;
    if(c!=undefined) this.c = c;
}

And I want to set only the value for b, then I could do something like this.

myFunc(undefined, "some Value for b", undefined);

But I was wondering if there was a way in which I could do something like this for setting the value of b

myFunc(b:"some value for b");

This is probably wrong, but you probably see what I mean. I want to specify the name of the property along with the value so that I do not have to worry about passing undefined values. How would I do this?

Also feel free to change the title, I have no idea what this question should be called.

5
  • This is a solution: ivanzuzak.info/2009/08/09/named-arguments-in-javascript.html Commented Mar 19, 2013 at 19:06
  • 1
    "I have no idea what this question should be called". Funniest stackoverflow moment of my life so far... Commented Mar 19, 2013 at 19:07
  • What's the intention with this.a etc? You're not instantiating an object, so this will be the global object. Commented Mar 19, 2013 at 19:09
  • @bfavaretto yes I am using this function for creating objects, even though not shown here Commented Mar 19, 2013 at 19:10
  • "Do you mean to say that the example I gave actually works?" Only if you change the syntax to use object literal notation (ie add {} around b:"some value". The key concept is that you're passing an Object as an argument. Objects can be created using the object literal notation, which looks like this: { propA: 'valueA', propB: 'valueB' } Commented Mar 19, 2013 at 19:20

3 Answers 3

4

jsFiddle Demo

Sure, just use this:

function myFunc(options)
{
 if(typeof(options['a']) != "undefined") this.a = options['a'];
 if(typeof(options['b']) != "undefined") this.b = options['b'];
 if(typeof(options['c']) != "undefined") this.c = options['c'];
}

and then call it with

var func = new myFunc({b:"some value for b"});
console.log(func.b);//"some value for b"
Sign up to request clarification or add additional context in comments.

8 Comments

@Ojay - You were correct, the options object needs to check for a string index and not a variable one.
@TravisJ I probably think that he meant that you are missing brackets
@AnkurSharma - Ah, the simple things. Fixed. ;)
@OJay I am wondering why obj[i] works and why not obj.i. Look here -jsfiddle.net/GyBdz . Just trying to understand what is going on here.
@AnkurSharma in the case of the for in loop, you are assigning the property name of the passed in object to the variable i. this[i] works because it replaces the i with the current property (i.e. b), so actually assigned this.b, whereas this.i is not using the variable i, you are actually creating a property of i on the function. try it, instead of console.log(func.b) with the second one, change it to console.log(func.i)
|
1

yes, pass in an object literal

function myFunc(options)
{
    if(options.a!=undefined) this.a = options.a;
    if(options.b!=undefined) this.b = options.b;
    if(options.c!=undefined) this.c = options.c;
}

and call like this

var var1 = new myFunc({b:"some value for b"});

or for a more dynamic, extensible way ( as mentioned)

function myFunc(obj)
{
    for (var i in obj)
    {
        if(obj.hasOwnProperty(i))
        {
            this[i]=obj[i];
        }
    }
}

var func = new myFunc({b:"some value for b"});
console.log(func.b);//"some value for b"

1 Comment

note that if called like that, it will attach b to the window object.
1

You can achieve this by passing an Ojbect as an argument:

var someObject = { a: 'some A value', b: 'some B value' };

myFunc(someObject);

This is a very common practice.

For example, jQuery uses this construct often.

If you look at the documentation for jQuery.animate(), you can see it accepts a Plain Object as the first argument.

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.