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