21.custom constructor


#1

function Person(name,age) {
this.name = name;
this.age = age;
}

// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);
// help us make george, whose name is "George Washington" and age is 275
var george = new Person() {
this.name = "george washington";
this.age = 275;
};
i got a error msg like ops, try again. There was a problem with your syntax. how to solve this


#2

you don't need to use the this. place holder ..
you need to write the line the same way as bob and susan was written:

function Person(name,age) {
this.name = name;
this.age = age;
}

// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);
// help us make george, whose name is "George Washington" and age is 275

var george = new Person("George Washington", 275);

The Object Constructor
We mentioned the term constructor back in section one, when we talked about making an object using the keyword new. A constructor is a way to create an object.

When we write bob = new Object( ); we are using a built-in constructor called Object. This constructor is already defined by the JavaScript language and just makes an object with no properties or methods.

This means we have to add our properties one at a time, just like we've been doing. To review, we've created bob using the constructor and defined the name property for you.

// here we make bob using the Object constructor
var bob = new Object();
bob.name = "Bob Smith";
// add bob's age here and set it equal to 20
bob.age = 20;

Custom Constructors
But this approach of adding in properties one at a time for every object is tedious! Instead of always using the boring Object constructor, we can make our own constructors.

This way we can set the properties for an object right when it is created. So instead of using the Object constructor which is empty and has no properties, we can make our own constructors which have properties.

function Person(name,age) {
this.name = name;
this.age = age;
}

To see how this works, look at our Person constructor.

This constructor is used to make Person objects. Notice it uses the keyword this to define the name and age properties and set them equal to the parameters given.

Now we can use this constructor to make our good friends bob and susan in only one line each!

Once we have the constructor, it's way easier to make people because we can include their name and age as arguments to their respective constructors.

// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);

// help us make george, whose name is "George Washington" and age is 275

var george = new Person("George Washington", 275);

So review this again....

// here we make bob using the Object constructor
var bob = new Object();
bob.name = "Bob Smith";
// add bob's age here and set it equal to 20
bob.age = 20;

function Person(name,age) {
this.name = name;
this.age = age;
}

// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);

*and so the way you wrote the code is not what is being asked in the task.
As you have created your own person function ....

function Person(name,age) {
this.name = name;
this.age = age;
}

you can save yourself work by just writing it this way:*

// help us make george, whose name is "George Washington" and age is 275
var george = new Person("George Washington", 275);