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 ?

1

3 Answers 3

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.

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

6 Comments

in checks the prototype. Are you sure that's safe?
@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 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';
@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.
@ArhamAliQureshi: Chrome doesn't have any problem with it: jsbin.com/ejiqet/1
|
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);
}

2 Comments

+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).
Probably also worth mentioning that this changes the object passed in.
0

Plesae examine This

and mark the correct answer which satisfies you.

function show(message){
  alert(message['head']);
}

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.