1

I'm learning javscript and trying to know more about object oriented programming.

i have a class called man:

var man = function() {

    this.name = "jack";

    this.walk = function(){
        console.log("im walking");
    };

};

i want create another class called hero that inherits from man containing all man class methods and properties

var hero = function(){

 // inherit from man and has it own methods

};

How to do that so i can create object contain methods from both of them.

1

1 Answer 1

0

After (and outside of) the hero function assign new man() to hero's prototype:

var man = function() {

    this.name = "jack";

    this.walk = function() {
        console.log("im walking");
    };
};

var hero = function() {
    // hero stuff
}

hero.prototype = new man();

// ...

var batman = new hero();

alert(batman.name) // jack

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

2 Comments

i think var hero = function() { man.call(this) } was the right answer why you change it ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.