相关疑难解决方法(0)

经典继承与javascript中的原型继承

我搜索了这么多链接,并且不能很好地了解经典继承和原型继承之间的区别?

我从中学到了一些东西,但我仍然对这些概念感到困惑.

经典继承

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

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

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); //call super constructor.
}

//subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Run Code Online (Sandbox Code Playgroud)

经典继承是否在内部使用原型继承?

http://aaditmshah.github.io/why-prototypal-inheritance-matters/

从上面的链接,我了解到我们不能在运行时在经典继承中添加新方法.它是否正确?但是你可以查看上面的代码我可以在运行时通过原型添加"move"方法和任何方法.那么这是基于原型的经典继承吗?如果是这样,什么是实际的经典继承和原型继承?我很困惑.

原型继承.

function Circle(radius) {
    this.radius = radius;
}
Circle.prototype.area = function () {
    var radius = this.radius;
    return Math.PI * …
Run Code Online (Sandbox Code Playgroud)

javascript inheritance

109
推荐指数
3
解决办法
5万
查看次数

标签 统计

inheritance ×1

javascript ×1