2

我正在尝试创建 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();

有没有更好的方法来创建函数HumanRobot这样我就不必写两次了?

我试过了

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();

但它没有用。

4

5 回答 5

1

您可以创建一个 mixin 函数,将属性添加到构造函数并使用适当的上下文调用它。你也可以使用一个对象而不是这么多参数:

function mixin(props) {
  for (var prop in props) {
    this[prop] = props[prop];
  }
}

function Human(props) {
  mixin.call(this, props);
}

function Robot(props) {
  mixin.call(this, props);
}

Human.prototype.scream = function() {
  console.log('Human has '+ this.legs.length +' legs'); 
};

Robot.prototype.scream = function() { 
  console.log('Robot has '+ this.legs.length +' legs'); 
};

var human = new Human({
  head: 1,
  body: 1,
  hand: 1,
  legs: [1,2],
  feet: [1,2]
});

var robot = new Robot({
  head: 1,
  body: 1,
  hand: 1,
  legs: [1,2,3],
  feet: [1,2,3]
});
于 2013-11-07T08:55:02.997 回答
1

我试过了

function Robot(hand,leg,head,body,foot1,foot2){
    Human(hand,leg,head,body,foot1,foot2);
}

这确实Human作为函数调用,而不是作为构造函数调用 - 不是在实例上,而是使用全局对象作为thisvalue。您需要使用.call

function Robot(hand,leg,head,body,foot1,foot2) {
    Human.call(this, hand,leg,head,body,foot1,foot2);
}

或者,如果您不想将它们写出来,您可以使用arguments对象apply

function Robot() {
    Human.apply(this, arguments);
}

但是,我建议不要从另一个构造函数中调用一个构造函数,而是将通用代码放入一个通用构造函数中并从两者中调用它Humanand Robot,以便您也可以将特定的实例初始化代码放入它们的构造函数中:

function Humanoid (hand, leg, head, body, foot1, foot2) {
    this.hand = hand;
    this.leg = leg;
    this.head = head;
    this.body = body;
    this.feet = [foot1, foot2]
}
function Human() {
    Humanoid.apply(this, arguments);
    …
}
// if you want Humans to inherit Humanoid prototype properties:
// Human.prototype = Object.create(Humanoid.prototype);
Human.prototype.… = …;

function Robot() {
    Humanoid.apply(this, arguments);
    …
}
// if you want Robots to inherit Humanoid prototype properties:
// Robot.prototype = Object.create(Humanoid.prototype);
Robot.prototype.… = …;

一种创建人类和机器人功能的方法,这样我就不必写两次了?

如果您确定构造函数代码始终完全相同,您也可以使用闭包:

function getConstructor() {
    return function Human(hand,leg,head,body,foot1,foot2) {
        this.hand = hand;
        this.leg = leg;
        this.head = head;
        this.body = body;
        this.feet = [foot1,foot2];
    }
}
var Human = getConstructor();
Human.prototype.… = …;
var Robot = getConstructor();
Robot.prototype.… = …;
于 2013-11-07T08:49:00.983 回答
0

What you need is apply or call

// with call
function Robot(hand,leg,head,body,foot1,foot2){
  Human.call(this, hand, leg, head, body, foot1, foot2);
}

var you = new Robot(1,2,3,4,5,6);
you.scream();

// with apply
function Robot(hand,leg,head,body,foot1,foot2){
  Human.apply(this, arguments);
}

var you = new Robot(1,2,3,4,5,6);
you.scream();

With apply you basically pass all the arguments in same order you got from the original function.

于 2013-11-07T08:47:15.607 回答
0

为什么不为 Human 和 Robot 创建一个通用原型,并且只覆盖尖叫功能:

function HumanRobotProt(hand,leg,head,body,foot1,foot2){

  this.hand = hand;
  this.leg = leg;
  this.head = head;
  this.body = body;
  this.feet = [foot1,foot2];
}

Human.prtotype = new HumanRobotProt;
Robot.prototype = new HumanRobotProt;

function Human(hand,leg,head,body,foot1,foot2){
    Human.call(this, hand,leg,head,body,foot1,foot2);
    this.scream = function(){ 
    alert("HUMANNN"); 
}

function Robot(hand,leg,head,body,foot1,foot2){
    Robot.call(this, hand,leg,head,body,foot1,foot2);
    this.scream = function(){ 
    alert("ROBOOOT"); 
}
于 2013-11-07T08:58:55.377 回答
0

更好的 OOP 方法

// From "Pro Javascript Design Patterns" by Ross Harmes and Dustin Diaz
function extend(subClass,superClass) {
    var F = function() {};
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = subClass;

    subClass.superClass = superClass.prototype;
    if(superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
}

function Humanoid(hand,leg,head,body,foot1,foot2){
    this.hand = hand;
    this.leg = leg;
    this.head = head;
    this.body = body;
    this.feet = [foot1,foot2];
}
function Robot() {
    Robot.superClass.constructor.apply(this,arguments);
}
extend(Robot,Humanoid);

function Human() {
    Human.superClass.constructor.apply(this,arguments);
}
extend(Human,Humanoid);

var John = new Human(2,2,1,1,1,1);
console.log(John);
于 2013-11-07T08:59:48.307 回答