2

これら 3 つの関数/オブジェクトは同じ目的で使用できます。各オブジェクトの新しいインスタンスを作成します。それぞれ1000個ずつ作るのでどれが一番性能がいいのか知りたいです。

jelly1 = new JellyFish();
jelly2 = new JellyFish2();
jelly3 = new JellyFish3();


//jellyfish object 3
function JellyFish3() {
    this.color = "blue";
    this.size = "medium";
    this.move = function (direction) {
        console.log("moving to " + direction);
        return direction;
    };
}

// jellyfish object 2
function JellyFish2() {};
// constructor
(function (instance) {
    instance.color = "blue";
    instance.size = "medium";
    instance.move = function (direction) {
        console.log("moving to " + direction);
        return direction;
    };
})(JellyFish2.prototype);

// jellyfish object 1
function JellyFish() {
    // constructor
    (function (instance) {
        instance.color = "blue";
        instance.size = "medium";
        instance.move = function (direction) {
            console.log("moving to " + direction);
            return direction;
        };
    })(JellyFish.prototype);
};
4

1 に答える 1