javascript基本对象,以使用不同的方法创建另外两个对象

javascript基本对象,以使用不同的方法创建另外两个对象,javascript,object,prototype,Javascript,Object,Prototype,多年来我一直在玩javascript,但现在我想认真一点。研究,并将其转化为对象 我想创建一个基础对象,并使用它创建另外两个稍有不同的对象 我认为这会奏效: function movingObject(x, y, z){ this.x = x; this.y = y; this.z = z; } var positiveMover = new movingObject(x, y, z); positiveMover.prototype.move = function(a, b

多年来我一直在玩javascript,但现在我想认真一点。研究,并将其转化为对象

我想创建一个基础对象,并使用它创建另外两个稍有不同的对象

我认为这会奏效:

function movingObject(x, y, z){
   this.x = x;
   this.y = y;
   this.z = z;
}

var positiveMover = new movingObject(x, y, z);
positiveMover.prototype.move = function(a, b){
    yadda yadda
}

var negativeMover = new movingObject(x, y, z);
negativeMover.prototype.move = function(b, a){
    adday adday
}

var pic = postiveMover(1, 2, 3);
pic.move(20, 10);

我在移动中遇到了一个未定义的错误……我很确定我的想法是错误的。任何建议都将不胜感激-信息链接,或谷歌的正确关键词

我认为这更像是两个类,你想要构建:

function movingObject(x, y, z){
   this.x = x;       this.y = y;       this.z = z;
}

// positive mover : child class of movingObject    
function positiveMover (x, y, z) {  
    // use parent class's constructor.
    movingObject.apply(this,arguments); 
};

// inherit parent's class.
positiveMover.prototype = Object.create(movingObject.prototype); 

positiveMover.prototype.move = function(a, b){   yadda yadda    }
但是,如果您寻求方法的每个实例选择,则可以执行以下操作:

function movingObject(x, y, z, movingMethod){
   this.x = x;       this.y = y;       this.z = z;
   this.move = movingMethod;
}
或者只设置移动对象的“移动”属性,从而覆盖默认原型:

function movingObject(x, y, z){
   this.x = x;       this.y = y;       this.z = z;
}
movingObject.prototype.move= function(a,b) { /*some default code*/}

var oneMover = new movingObject(0,0,0);
oneMover.move = function(a,b) { /* some specific code */ };

在javascript中,
x.prototype
不是“x的原型”。我想在negativeMover对象中添加移动功能,谢谢游戏炼金术士!对等实例选择实际上是我所追求的,但我也很欣赏其他两个选项的经验教训