两个javascript对象具有相同的实现属性但不同原型的方式

时间:2013-11-07 08:39:42

标签: javascript oop

我正在尝试创建两个共享相同实现方式的对象:

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

但它不起作用。

5 个答案:

答案 0 :(得分:1)

  

我试过

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

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

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

或者,如果您不想写出来,可以使用arguments objectapply

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

但是,我建议将公共代码放入通用构造函数并从HumanRobot调用,而不是从另一个构建函数中调用,这样您就可以放置特定的实例 - 其构造函数中的初始化代码:

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.… = …;

答案 1 :(得分: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]
});

答案 2 :(得分:0)

您需要的是applycall

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

使用apply,你基本上按照从原始函数获得的顺序传递所有参数。

答案 3 :(得分:0)

为什么不为Human和for Robot创建一个公共原型,只覆盖scream函数:

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"); 
}

答案 4 :(得分: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);