3

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.

1
  • 1
    You can pass and then read an object to the constructor; new Employee({name: "bob", age: 40}); Commented May 28, 2013 at 10:56

3 Answers 3

6

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.

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

Comments

1
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 || "");
}

1 Comment

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.
-1

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");

2 Comments

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.
yep...you're right. Sorry about that. I'm just gettin to understand how the variables actually work in js

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.