我正在尝试创建 2 个共享相同实现方式的对象:
function Human(hand,leg,head,body,foot1,foot2){
this.hand = hand;
this.leg = leg;
this.head = head;
this.body = body;
this.feet = [foot1,foot2];
}
function Robot(hand,leg,head,body,foot1,foot2){
this.hand = hand;
this.leg = leg;
this.head = head;
this.body = body;
this.feet = [foot1,foot2];
}
我希望他们有不同的原型:
Human.prototype.scream = function(){
alert("HUMANNN");
//some other functions
};
Robot.prototype.scream = function(){
console.log("ROBOOBOT");
//some other functions
};
var Tom = new Robot(1,2,3,4,5,6);
Tom.scream();
var I = new Human(312314123,2141123,213131412,4121312,132124,12313);
I.scream();
有没有更好的方法来创建函数Human
,Robot
这样我就不必写两次了?
我试过了
function Robot(hand,leg,head,body,foot1,foot2){
Human(hand,leg,head,body,foot1,foot2);
}
var Micky = new Robot(1,2,3,4,5,6);
Micky.scream();
但它没有用。