0

I am sorry i am not a good in this, but I want create a function like this

function show (message['head':'error','body':'msg'])
{ // 'error' and 'msg' and the default values
alert(message[head]);

}

show ({'head' : 'this is head', 'body' : 'this is body'});

What is the correct way to above method work properly ?

  • related or even a duplicate: http://stackoverflow.com/questions/10813852/concise-way-to-accept-an-options-object-as-an-argument-falling-back-to-defaults – Felix Kling Feb 02 '13 at 13:07

3 Answers3

4

Like this:

function show (message)
{
    alert(message.head);
    // or
    alert(message['head']); // Note the quotes
}

Your call to it is fine.

To supply defaults, as David points out, you can use the curiously powerful || operator:

function show (message)
{
    var head = message.head || 'error',
        body = message.body || 'msg';

    alert(head);
}

(The above is slightly different from David's approach in that it avoids changing the message object that was passed in, which is usually not a good idea as the function doesn't own the object, the caller does.)

That works because if head (for example) isn't on the message object at all, it's falsey, and so head = message.head || 'error' ends up assigning 'error' to head. This is a handy trick, but there's a gotcha: If head could have a falsey value already, and that's valid, you don't want to use the || trick. Instead, you can use in to check:

function show (message)
{
    var head = 'head' in message ? message.head : 'error',
        body = 'body' in message ? message.body : 'msg';

    alert(head);
}

That will use the value from message if it's present, regardless of whether it's falsey.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • `in` checks the prototype. Are you sure that's safe? – David G Feb 02 '13 at 13:07
  • @David: I think it's appropriate here, yes. Otherwise we're in the odd position of using the default even though if we look at `message.head` we'd see a different value (if `message` is an object with `head` on its prototype). There's lots of places where you want `hasOwnProperty` instead, but to me, this isn't one of them. – T.J. Crowder Feb 02 '13 at 13:13
  • @T.J.Crowder Thanks for your help. But What should I do if I don't create 'new variable'. For example: message.head = 'head' in message ? message.head : 'error'; – Arham Ali Qureshi Feb 02 '13 at 19:17
  • 1
    @ArhamAliQureshi: You can certainly do that (`message.head = 'head' in message ? message.head : 'error';`) if you like, just be aware that that's the kind of thing that can have unexpected side-effects (because you're modifying the `message` object that was passed to you). I've seen a *lot* of bugs over the years because people weren't expecting a function to modify something it received as an argument. Just be clear in your documentation of the function. But why wouldn't you use a variable? I'm not seeing the downside of doing that. – T.J. Crowder Feb 02 '13 at 19:23
  • @T.J.Crowder I tried that but I guess chrome is not letting me to do that. So I used you recommended technique. I was trying to do that because I usually don't like to create multiple variables. But you are the Boss :) – Arham Ali Qureshi Feb 02 '13 at 19:47
  • 1
    @ArhamAliQureshi: Chrome doesn't have any problem with it: http://jsbin.com/ejiqet/1 – T.J. Crowder Feb 02 '13 at 20:12
1

If an object doesn't have a property, access of that property will return the value undefined. We can use this along with the logical OR || operator to assign default values to the object.

function show(message) {
    message.head = message.head || 'error';
    message.body = message.body || 'msg';

    alert(message.head);
}

If message.head is undefined, it will assign "error" to the object property, or it will otherwise retain its value.


As Crower pointed out, this has a potential "gotcha" as an empty string can be deduced as a falsy value causing the unwanted assignment of a default value. Use this version as it checks if the property is actually on the object:

function show(message) {
    message.head = message.hasOwnProperty('head') ? message.head : 'error';
    message.body = message.hasOwnProperty('body') ? message.body : 'msg';

    alert(message.head);
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    +1 I missed the bit about default values. You might want to warn about the "gotcha" with regard to falsey property values (e.g., if `message.head` can meaningfully be `0` or `""`, you don't want to indiscriminately do `|| 'error'` on it). – T.J. Crowder Feb 02 '13 at 12:58
  • 1
    Probably also worth mentioning that this *changes* the object passed in. – T.J. Crowder Feb 02 '13 at 13:02
0

Plesae examine This

and mark the correct answer which satisfies you.

function show(message){
  alert(message['head']);
}
Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44