javascript中的构造函数和从超类继承属性

javascript中的构造函数和从超类继承属性,javascript,inheritance,Javascript,Inheritance,我有一个关于用javascript演示继承的代码的问题。 代码基于 您可以在此处看到实时演示: 代码如下: //形状-超类 函数形状(){ 这个.x=1; 这个y=2; } //超类方法 Shape.prototype.move=函数(x,y){ 这个.x+=x; 这个.y+=y; console.info('Shape moved')、this.x、this.y); }; //矩形子类 函数矩形(){ Shape.call(this);//调用超级构造函数。 console.log(“###

我有一个关于用javascript演示继承的代码的问题。 代码基于

您可以在此处看到实时演示:

代码如下:

//形状-超类
函数形状(){
这个.x=1;
这个y=2;
}
//超类方法
Shape.prototype.move=函数(x,y){
这个.x+=x;
这个.y+=y;
console.info('Shape moved')、this.x、this.y);
};
//矩形子类
函数矩形(){
Shape.call(this);//调用超级构造函数。
console.log(“#########################”内矩形.console.log;
}
//子类扩展了超类
Rectangle.prototype=Object.create(Shape.prototype);
//如果不将Rectangle.prototype.constructor设置为Rectangle,
//它将采用形状(父级)的prototype.constructor。
//为了避免这种情况,我们将prototype.constructor设置为Rectangle(child)。
Rectangle.prototype.constructor=矩形;
var rect=新矩形();
log('rect是矩形的实例吗?',rect instanceof Rectangle);//真的
log('rect是Shape的实例吗?',rect instanceof Shape);//真的
直线移动(1,2);//输出,“形状移动”。2, 4'

直线移动(1,2);//输出,“形状移动”。3,6'
至于你的第一个问题,
Rectangle.prototype.constructor=Rectangle
没有效果。该行设置矩形类的构造函数,但是
Rectangle()
函数已经是构造函数,因此没有效果

对于第二个问题,move函数通过传递到函数中的x值更改对象的x值。y值也是如此。2+1=3和4+2=6

如果希望x和y值每次翻倍,可以使用此函数:

Shape.prototype.move = function() {
    this.x += this.x; // <-- uses this.x instead of x parameter
    this.y += this.y; // <-- uses this.y instead of y parameter
    console.info('Shape moved.',this.x,this.y);
};
Shape.prototype.move=function(){

this.x+=this.x;//至于你的第一个问题,
Rectangle.prototype.constructor=Rectangle
没有效果。那一行设置了Rectangle类的构造函数,但是
Rectangle()
函数已经是构造函数了,所以没有效果

对于第二个问题,move函数通过传递给函数的x值更改对象的x值。y值也是如此。2+1=3和4+2=6

如果希望x和y值每次翻倍,可以使用此函数:

Shape.prototype.move = function() {
    this.x += this.x; // <-- uses this.x instead of x parameter
    this.y += this.y; // <-- uses this.y instead of y parameter
    console.info('Shape moved.',this.x,this.y);
};
Shape.prototype.move=function(){
我现在明白了

//形状-超类
函数形状(){
这个.x=1;
这个y=2;
}
//超类方法
Shape.prototype.move=函数(){
这个.x+=这个.x;
this.y+=this.y;
console.info('Shape moved')、this.x、this.y);
};
//矩形子类
函数矩形(){
Shape.call(this);//调用超级构造函数。
console.log(“#########################”内矩形.console.log;
}
//子类扩展了超类
Rectangle.prototype=Object.create(Shape.prototype);
//如果不将Rectangle.prototype.constructor设置为Rectangle,
//它将采用形状(父级)的prototype.constructor。
//为了避免这种情况,我们将prototype.constructor设置为Rectangle(child)。
Rectangle.prototype.constructor=矩形;
var rect=新矩形();
log('rect是矩形的实例吗?',rect instanceof Rectangle);//true
log('rect是Shape的实例吗?',rect instanceof Shape);//true
rect.move();//输出'Shape moved'
rect.move();//输出'Shape moved'
这将输出1,2->2,4->4,8->…并且不需要参数移动()

在原始代码中,
this.x
this.y
仍然存在。很抱歉造成混淆,非常感谢

下面是一个工作代码:

// Shape - superclass
function Shape() {
  this.x = 1;
  this.y = 2;
}

// superclass method
Shape.prototype.move = function(a) {
  this.x = this.x + a;
  this.y = this.y + a;
  console.info('Shape moved.',this.x,this.y);
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); // call super constructor.
  console.log("###### Inside Rectangle.constructor ######");
}

// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);

//If you don't set Rectangle.prototype.constructor to Rectangle,
//it will take the prototype.constructor of Shape (parent).
//To avoid that, we set the prototype.constructor to Rectangle (child).
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?', rect instanceof Shape); // true
rect.move(3); // x: 4, y: 5
rect.move(3); // x: 7, y: 8
rect.move(3); // x: 10, y: 11
请注意,
this.x
this.y
是持久的。

我现在明白了

//形状-超类
函数形状(){
这个.x=1;
这个y=2;
}
//超类方法
Shape.prototype.move=函数(){
这个.x+=这个.x;
this.y+=this.y;
console.info('Shape moved')、this.x、this.y);
};
//矩形子类
函数矩形(){
Shape.call(this);//调用超级构造函数。
console.log(“#########################”内矩形.console.log;
}
//子类扩展了超类
Rectangle.prototype=Object.create(Shape.prototype);
//如果不将Rectangle.prototype.constructor设置为Rectangle,
//它将采用形状(父级)的prototype.constructor。
//为了避免这种情况,我们将prototype.constructor设置为Rectangle(child)。
Rectangle.prototype.constructor=矩形;
var rect=新矩形();
log('rect是矩形的实例吗?',rect instanceof Rectangle);//true
log('rect是Shape的实例吗?',rect instanceof Shape);//true
rect.move();//输出'Shape moved'
rect.move();//输出'Shape moved'
这将输出1,2->2,4->4,8->…并且不需要参数移动()

在原始代码中,
this.x
this.y
仍然存在。很抱歉造成混淆,非常感谢

下面是一个工作代码:

// Shape - superclass
function Shape() {
  this.x = 1;
  this.y = 2;
}

// superclass method
Shape.prototype.move = function(a) {
  this.x = this.x + a;
  this.y = this.y + a;
  console.info('Shape moved.',this.x,this.y);
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); // call super constructor.
  console.log("###### Inside Rectangle.constructor ######");
}

// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);

//If you don't set Rectangle.prototype.constructor to Rectangle,
//it will take the prototype.constructor of Shape (parent).
//To avoid that, we set the prototype.constructor to Rectangle (child).
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?', rect instanceof Shape); // true
rect.move(3); // x: 4, y: 5
rect.move(3); // x: 7, y: 8
rect.move(3); // x: 10, y: 11

请注意,
this.x
this.y
是持久的。

这两次,你都要求它移动
(1,2)
。我不确定你为什么期望结果会不同。我想做多次移动,并且x和y的值会持久。如果我们移动(1,2)在这种情况下,它们不止一次给出了不同的值。要回答第一个问题,请查看
x
y
的值是否持续。将其分解;从
x:1,y:2
开始,然后添加
(1,2)
这就是
x:1+1=2
y:2+2=4
。现在的值是
x:2,y:4
。然后添加
(1,2)
我再问一遍,为什么你认为它们会不同?请编辑你的问题,解释你认为它们应该是什么,以及为什么你认为这两次你都是在要求它移动
(1,2)
。我不知道你为什么期望结果会不同。我想做多次移动,并且x和y的值保持不变。如果我们这样做