0

I have 2 class: animal(parent) and dog(the 'child' of animal), when I create an object of Animal, and try to alert the name of the animal, I got undefined , and not her real name. Why? (sorry about the double post )

function Animal(name, year){
    alert(name);
    this.name = name;
    this.year = year;

    this.age = function (){
        var n = new Date().getFullYear();
        return n - this.year;
    };
}

function Dog(name, year, color, type) {
    this.color = color;
    this.type = type;
    Animal.call(this, name, year);

    //override method age of animal
    this.age = function (){
        var n = new Date().getFullYear();
        return (n - this.year) * 7;
    };

    Dog.prototype.age();
}

Dog.prototype = new Animal();

(This js class named: JsClass.js)

and in HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <script src="JsClass.js"></script>

        <script type="text/javascript">
            var p1 = new Animal("rex",2008);
        </script>
        <title>Insert title here</title>
    </head>
    <body>
    </body>
</html>

(using Eclipse)

Thank you!

2
  • For reference, you'd typically call the parent constructor before setting your own fields, rather than after. Shouldn't cause the problem you're seeing, though. Commented Nov 25, 2013 at 23:42
  • now I see that if i delete this line: Dog.prototype = new Animal(); it's alert the animal name. so what's the problem with this line? where should I put it? Commented Nov 25, 2013 at 23:49

3 Answers 3

1
function Animal(name , year){
    alert(name);
    this.name = name;
    this.year=year;

    this.age= function (){
        var n= new Date().getFullYear();
        return n-this.year;
    };
}
    function Dog(name , year , color , type) {      
        this.color=color;
        this.type=type;
        Animal.call(this, name,year);

        //override method age of animal
        this.age= function (){
            var n= new Date().getFullYear();
            return (n-this.year)*7;
        };

        Dog.prototype.age();
    }

// you invoke Animal as constructor. but you did not pass any argument to it . so name any year will be undefined in Animal() so alert will show undefined
    Dog.prototype = new Animal();

//  here is the normal invoke so alert will show rex
var p1 = new Animal("rex",2008);

your code will invoke Animal twice, the first invoke alert undefined and the second alert rex

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

Comments

1

when I create an object of Animal, and try to alert the name of the animal, I got undefined , and not her real name

Your alert function is in the animal constructor function. You didn't pass anything into that function when you instantiated it as the prototype of Dog.

Here's what's happening:

Dog.prototype = new Animal(); calls the constructor function without any parameters (hence the undefined alert) and sets the prototype of dog to the Animal instance.

new Animal("rex", 2008); calls the constructor again. This time, with a name, so you'll get "rex".

I recommend setting the methods as properties of the prototype. That way, the functions don't get created on every instance. I would change your code to look like this:

function Animal(name, year) {
    this.name = name;
    this.year = year;
};

Animal.prototype.age = function() {
    var n = new Date().getFullYear();
    return n - this.year;
};

function Dog(name, year, color, type) {
    Animal.call(this, name, year);
    this.color = color;
    this.type = type;
};

// Override method age of animal
Dog.prototype.age = function() {
    var n = new Date().getFullYear();
    return (n - this.year) * 7;
};

Dog.prototype = new Animal();

But there's still one problem - you're invoking the Animal constructor twice. Once when you set it to be the prototype of Dog, and another when you do Animal.call(). One way around this is to use Crockford's method of inheritance.

2 Comments

thanks. but which parameters should I insert into this line: Dog.prototype = new Animal('wow'); ?
Should just be Dog.prototype = new Animal();
0

In

Dog.prototype = new Animal();

You are executing Animal() without parameters. And its OK for simulate inheritance in javascript.

Comments

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.