继承自 class 构造函数 javascript
inherit from a class constructor javascript
我正在学习 javscript 并试图了解更多关于面向对象编程的知识。
我有一个叫 man
的 class:
var man = function() {
this.name = "jack";
this.walk = function(){
console.log("im walking");
};
};
我想创建另一个名为 hero
的 class,它继承自 man
,包含所有 man
class 方法和属性
var hero = function(){
// inherit from man and has it own methods
};
如何做到这一点,以便我可以创建对象包含它们的方法。
在 hero 函数之后(和之外)将 new man()
分配给 hero 的 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
我正在学习 javscript 并试图了解更多关于面向对象编程的知识。
我有一个叫 man
的 class:
var man = function() {
this.name = "jack";
this.walk = function(){
console.log("im walking");
};
};
我想创建另一个名为 hero
的 class,它继承自 man
,包含所有 man
class 方法和属性
var hero = function(){
// inherit from man and has it own methods
};
如何做到这一点,以便我可以创建对象包含它们的方法。
在 hero 函数之后(和之外)将 new man()
分配给 hero 的 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