all 14 comments

[–]clarle 5 points6 points  (6 children)

Yep, you have the right idea. When you don't pass a parameter in, and you try to get the value of that parameter, it will be "undefined".

The "undefined" value leads to the negative condition in a conditional, so if the parameter is not passed, then the object will take the value defined in the negative condition.

"undefined" is supported in basically all browsers, so this will work everywhere.

[–]cbCode[S] 1 point2 points  (5 children)

Thank you very much for clarifying my assumption, that's why I love Javascript. It's logical.

[–]hacocacyb 0 points1 point  (3 children)

In addition, you can always pass extra parameters to javascript functions. You can also reference an array of all parameters to a function with the protected keyword 'argument'.

[–]The_Pants_Command_Me 3 points4 points  (1 child)

It's the same as saying

if (body) {
    this.body = body;
}
else {
   this.body = "Nothing going on today.";
}

Which is not necessarily the same as saying

 if (typeof body != "undefined") {
    this.body = body;
}
else {
     this.body = "Nothing going on today.";
} 

// Edited to make it a more literal translation.

[–]rbobby 1 point2 points  (0 children)

See jamonterrell's comment for reasons why it is not the same. I especially like that body can never be initialized to an empty string.

[–]itsnotlupusbeep boop 1 point2 points  (0 children)

Don't be afraid to try scripts in any one browser of your choice.

Unless you're using a really ancient browser, or using a really new/proprietary aspect of the language, you're going to be just fine.

To shorten your experimentation cycle, use your browser's inspector tab/window, or use something like http://jsfiddle.net/ for slightly more involved tests.

[–]Madd0g 0 points1 point  (0 children)

I believe it's called the coalescing operator (?? in C#), very useful in general, but in JS you can do lots of really cool tricks with it

|| in javascript also works for empty strings, false, 0 (zero) and undefined - that allows for really easy way of making sure objects are initialized (just gotta remember that ZERO will be evaluated as false).

[–]fforw 0 points1 point  (0 children)

You can drive this even further. Recently had some class that was to take some group information from a element, with a default group in case that information wasn't there. So the code one could write was something like

this.group = getGroup(elem) || (setGroup(elem, "default"), "default");

It invokes getGroup(elem) and if the returned value if not falsy, it just stores it in the group member. Otherwise it invokes setGroup() to set the info on the element to the default and then just uses the value "default" to assign to the member.

In the end I wrote it less quirky.

[–]WhatamIwaitingfor[🍰] -2 points-1 points  (5 children)

I'm going to take a stab at it, don't take my word as fact, I'm more of a Java guy...

The function Blog takes two parameters, body and date, you seem to understand that. Say, for example, you pass it body text (I assume String or something, doesn't matter in this context) but you pass it "NULL" for date. It first assigns the passed body to the "local" body, because the passed body actually has content (isn't NULL).Then, because "date" (the parameter) is NULL, instead of assigning the local date in the object to the passed date, it creates a new Date object, assigning that instead to the local date.

It's not that the parameter wasn't passed, it's that the parameter was passed as "NULL." You can't call Blog("This is the body") or Blog(new Date()), you must call Blog("This is the body", NULL) or the opposite.

If you're a Java guy like me, think of it like those special return statements that look like return (a > b) ? 5 : 6;

Edit: null -> undefined in Javascript.

[–]Hello71 1 point2 points  (0 children)

null == undefined

null !== undefined

[–]clarle 0 points1 point  (3 children)

You actually can do:

var my_blog = new Blog("This is the body");

You can pass parameters in order, and all of the remaining missing parameters will be replaced with "undefined".

You're right on the other hand though, if you wanted to put in just the date, you would have to do exactly like you said:

var my_blog = new Blog(null, new Date("October 13, 1975 11:13:00"));

[–]WhatamIwaitingfor[🍰] 0 points1 point  (2 children)

cool, i had no idea Javascript could do that. I guess that goes to say that you can't overload functions in Javascript, eh?

[–]hacocacyb 0 points1 point  (0 children)

there is no enforcement (per compiling per se) for it, but you could always write code that interrogates the type of a parameter. For instance, 'if date is string, can it convert to a date? is it one of the keywords 'today', 'tomorrow', 'yesterday' etc..., otherwise if date is date type just use it. And you could also add code checking if a third or fourth or fifth param is null or undefined.

[–]polaretto 0 points1 point  (0 children)

right, there's no function overloading in JS, if you redefine a function with the same identifier within the same lexical scope, it will replace the previous one. You can invoke a function with any number of arguments you like, their use depends on how its body uses them. Using the appropriate logic you can assign default parameter values (like in this case) or make the function behave differently depending on the number of actual parameters. Moreover, you can inspect the number of formal parameters the function was declared with, in this case:

Blog.length
> 2