Introduction to Objects I 32. Customizing Constructors


#1

I don't know what is wrong with my code

// Use our new constructor to make the_hobbit in one line
var book = new Book(pages, author);
this.pages = pages;
this.author = author;

var the_hobbit = new Book(320,J.R.R. Tolkien);


#2

It's the constructor and the call that are wrong. Here is how it should look like.

// A custom constructor for book
function Book (pages, author) {
    this.pages = pages;
    this.author = author;
}

// Use our new constructor to make the_hobbit in one line
var the_hobbit = new Book(320, "J.R.R. Tolkien");

Now I didn't see if you even written a constructor, since you didn't show that part of your code, but what you written is wrong.
If you are not using constructor, then you can specify object.property = "some_value";. If you are using constructor, then you make is a function (check my constructor in code I've provided). There you can specify what values to assign to what property.
Next, you had an error in your constructor call. You tried to pass 320 as first and J.R.R. Tolkien as second argument. First can pass, since it's a number, but the second one...not so much. J.R.R. Tolkien is nothing. Look how I've wrapped it in quotation marks to make it a string. var the_hobbit = new Book(320, "J.R.R. Tolkien"); is the right way to call your constructor. We are sending a number of pages and string with name of author to constructor in this case. If you need more help, let me know and I'll try to clarify things a bit more.


#3

Thanks.

I got mixed up beccause I missunderstood the Hint:

Hint

                To make a new book with the constructor we use:

var name = new Book(pages, author);

For this example we want our book to have a pages of 320, and author of "J.R.R. Tolkien".


#4

Thanks ,it work exelent !