2

这三个函数/对象可以用于相同的目的,我将为每个对象创建新实例。我将分别创建 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

2

为创建 50 个构造函数... JellyFish1 获胜!http://jsperf.com/jellyfish

用于创建 50 个实例... JellyFish2 获胜!http://jsperf.com/jellyfish-instance

于 2013-07-26T08:41:11.040 回答