Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is my object construction,

function Employee(name, dob) {
       this.Name = name;
       this.DateOfBirth = new Date(dob);
   }

Now, I have created an instance for this, like

var emp = new Employee("sample","12/12/12");

Its working fine when i print the output.

But, if i create the object like

var emp = new Employee(name = "sample");

or

var emp = new Employee(dob = "12/12/12");

its not working fine. In both the cases, the DateOfBirth field is invalid.

I need to define an object with optional parameters.

share|improve this question
1  
You can pass and then read an object to the constructor; new Employee({name: "bob", age: 40}); –  Alex K. May 28 '13 at 10:56

3 Answers 3

up vote 6 down vote accepted

JavaScript does not support named optional parameters.

When you do var emp = new Employee(name = "sample");

You're declaring a name global variable, assigning sample to it and passing that to the new call.

You can use objects to accomplish similar syntax in JS:

var emp = new Employee({name:"sample"}); 

Where the Employee function becomes:

function Employee(options) {
       this.Name = options.name;
       if(options.dob !== undefined){
           this.DateOfBirth = new Date(options.dob);
       }
}

Worth mentioning, in practice, you often don't need an Employee class, and can simply do:

var emp = {name:"sample"};

Or:

var emp = {dob:new Date("12/12/12");}

So unless Employee grows to become a real model (and has more than just two fields) I think that you might want to consider that.

share|improve this answer
function Employee(params) {
    if (typeof params != "undefined") {
        this.Name = (typeof params.name != "undefined") ? params.name : "";
        this.DateOfBirth = (typeof params.dob != "undefined") ? new Date(params.dob) : null;
    }
}

new Employee({
    name: "John",
    dob: "12/12/12"
});
new Employee({
    name: "John"
});
new Employee({
    dob: "12/12/12"
});

or using simple statements using ||.

function Employee(params) {
    params = params || {};
    this.Name = params.name || "";
    this.DateOfBirth = new Date(params.dob || "");
}
share|improve this answer
    
typeof is an operator, not a function. Please consider typeof name !== "undefined" . Also, I think there is a big difference between setting default values (like the empty string in your case) and keeping the values undefined. Last thing, your first example does not work (You're setting to null but checking against undefined which are completely different), please consider revising those things. –  Benjamin Gruenbaum May 28 '13 at 11:04
    
thanks......... –  tracevipin May 28 '13 at 11:14

As a good practice, you should never leave out variables. You can explicitly call var emp = new Employee(null, "12/12/12");

This way everything is initialized and you will not havea headaches later on. There is also something like this, but you really need to check the values before assigning.

function Employee() {
       this.Name = name;
       this.DateOfBirth = new Date(dob);
   }

var emp = new Employee(name = null,dob = "12/12/12");
share|improve this answer
    
You're defining two globals here! You're making a name global and a dob global. Also OP wanted a clear syntax. Also, in JavaScript, null and undefined do not mean the same thing. –  Benjamin Gruenbaum May 28 '13 at 10:58
    
yep...you're right. Sorry about that. I'm just gettin to understand how the variables actually work in js –  adi May 28 '13 at 11:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.