在性能和所創建的數據結構而言,是:Is設置函數變量一樣使用對象文本
function coords(xpos, ypos, zpos){
this.xpos = xpos;
this.ypos = ypos;
this.zpos = zpos;
return this;
}
var coordinates = coords(0, 0, 0); // notice I am not calling new
同爲:
function coords(xpos, ypos, zpos){
return {
xpos : xpos,
ypos : ypos,
zpos : zpos,
};
}
var coordinates = coords(0, 0, 0);
是有更好的性能的方式生成coordinates
假設我有很多。
您的意思是'this.xpos = xpos; ...'和'var coordinates = new coords(0,0,0);'?如果不是,那麼這兩個功能就會完成不同的事情。 – maerics 2012-03-15 15:18:56
在這種情況下,最快的方式是完全不使用功能,例如, 'var coordinates = {xpos:0,ypos:0,zpos:0};'測試這兩種類型的對象創建的速度都需要一個基準,但我的猜測是這種差異不會有太大的不同,因爲兩者創建一個對象,並通過一個函數調用來調用。 – SJFrK 2012-03-15 15:19:21
@maerics謝謝 – zode64 2012-03-15 15:28:26