如何“扩展”javascript“class”并添加新内容?

时间:2015-05-10 11:25:27

标签: javascript parent-child

好的,所以我要做的就是制作一个Projectile类,然后创建一个BasicShot类,以Java的方式“扩展”Projectile。

我想将父级的变量设置为父类,并且在子类中设置任何新的变量。

var Projectile = function(x, y){
     this.x = x;
     this.y = y;
}

Projectile.prototype.update(){
     console.log("should not be called");
}

Projectile.prototype.checkIfWallHit(){
     console.log("should be called");
}

然后是儿童班。

var BasicShot = new function(x, y, left){
     this = new Projectile(x,y);
     this.left = left;
}

BasicShot.prototype.update(){
     console.log("should be called");
}

但是当我尝试这样做时,当我尝试制作一个BasicShot对象时,它说“BasicShot未定义”。

那么,我如何扩展我的Projectile类?

3 个答案:

答案 0 :(得分:2)

以下是基本模式,请参阅评论:



// === Projectile

// The constructor
function Projectile(x, y) {
    // Do things to initialize instances...
    this.x = x;
    this.y = y;
}

// Add some methods to its prototype
Projectile.prototype.update = function() {
    // This is a method on `Projectile`
    snippet.log("Projectile#update");
};
Projectile.prototype.checkIfWallHit = function(){
    snippet.log("Projectile#checkIfWallHit");
};

// ==== BasicShot

// The constructor
function BasicShot(x, y, left) {
    // Give Projectile a chance to do its thing
    Projectile.call(this, x, y);
    this.left = left;
}
// Hook it up to `Projectile`
BasicShot.prototype = Object.create(Projectile.prototype);
BasicShot.prototype.constructor = BasicShot; // JavaScript does this by default, so let's maintain it when replacing the object

// Add methods to its prototype
BasicShot.prototype.update = function() {
    snippet.log("BasicShot#update");
};

// === Usage
snippet.log("Using Projectile");
var p = new Projectile(1, 2);
p.update(); // Projectile#update
p.checkIfWallHit(); // Projectile#checkIfWallHit

snippet.log("Using BasicShot");
var c = new BasicShot(1, 2, 3);
c.update(); // BasicShot#update
p.checkIfWallHit(); // Projectile#checkIfWallHit

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
&#13;
&#13;
&#13;

从ES6开始,JavaScript的下一个版本将更加简单>

// REQUIRES ES6!!
class Projectile {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    update() {
        snippet.log("Projectile#update");
    }
    checkIfWallHit() {
        snippet.log("Projectile#checkIfWallHit");
    }
};

class BasicShot extends Projectile {
    constructor(x, y, left) {
        super(x, y);
        this.left = left;
    }
    update() {
        snippet.log("BasicShot#update");
    }
}

它与上面的第一个例子做同样的事情,新的语法只是语法糖。但它真的很好吃糖。

答案 1 :(得分:-1)

你可以这样做

http://jsfiddle.net/jigardafda/0qkevb0q/2/

var factory = {};

factory.getProjectileObject = function(xarg, yarg){

    var Projectile = function(x, y){
         this.x = x;
         this.y = y;
    }

    Projectile.prototype.update = function() {
         console.log("should not be called");
    }

    Projectile.prototype.checkIfWallHit = function() {
         console.log("should be called");

    }

    return new Projectile(xarg, yarg);
}

factory.getBasicshotObject = function(xarg, yarg, leftarg) {

    var BasicShot = function(left){
         this.left = left;
    }

    // Extending parents methods
    BasicShot.prototype = factory.getProjectileObject(xarg, yarg);
    BasicShot.prototype.constructor = BasicShot;

    BasicShot.prototype.update = function(){
         console.log("should be called");
    }

    return new BasicShot(leftarg);
}

i = factory.getBasicshotObject(12,34, 23);
console.log(i);

答案 2 :(得分:-1)

实现这种继承的最佳方法是使用原型继承。

一种通用做法是将全局函数与Object相关联,并使用它来创建派生对象。 (因为全球性很糟糕,还有办法绕过这个)。

Object.prototype.begetObject = function () {
    function F() {}
    F.prototype = this;
    return new F();
};

BasicShot= Projectile.begetObject();

您可以像在问题中提出的那样覆盖/扩展此basicShot对象的功能。

BasicShot.prototype.update = function (){
     console.log("should be called");
};

我强烈建议您阅读这篇关于原型继承的文章,绝对值得。 http://javascript.crockford.com/prototypal.html