Search code examples
javascriptextendphaser-framework

Add prototype function without using .prototype= in each line


I would like to know if there's any way that I can add prototype function without using .prototype= in each line

This is the current code:

Fruit= function( x, y, settings) {
    Phaser.Sprite.call(this,game,x,y, 'fruit');
    game.add.existing(this);
};

Fruit.prototype.basic= function() {}

Fruit.prototype = Object.create(Phaser.Sprite.prototype);
Fruit.prototype.constructor = Fruit;

//I find that writing function in the following way is very hard to focus and find what I need immediately

Fruit.prototype.move= function() {

};

Fruit.prototype.fall= function() {

};

I want to write my code in this way, but I need to inherit from the original Phaser prototype. Any way I can write the code in the way below while still inherit from Phaser.Sprite.prototype?

Fruit.prototype = {
    move: function () {
    },
    fall: function () {
    }
}

As long as I can write it in this way is fine:

move: function () {
},

fall: function () {
}

Thanks


Solution

  • To my understand you want to apply a set of new methods from one object to a prototype object at once.

    You could do this via Object.assign():

    Object.assign(Fruit.prototype, {
        move: function () {
        },
    
        fall: function () {
        }
    });
    

    This will add all properties from the 2nd to n-th parameter to the object in the 1st parameter passed to assign().