0
了以下兩種方式,哪種格式定義對象是很好的考慮到性能和使用情況:Javascript對象定義
//Object created with public members defined using this.
var object1 = function() {
var private_i = null;
this.public_j = null;
//public function
this.public_func = function() {
}
}
OR
//Object created with public members defined using return patterns.
var object2 = function() {
var private_i = null,
public_j = null,
//private function will be exposed from return statement.
_public_func = function() {
};
return {
public_func : _public_func
};
}
克羅克福德建議廢除'this'。 https://www.youtube.com/watch?v=PSGEjv3Tqo0 – m59 2015-03-02 07:45:18
第一個更簡單,處理更少。您可以嘗試檢查jsperf的性能。 :) – frogcoder 2015-03-02 07:46:44
這2個片段不會產生相同的結果,因此它們無法比擬。 – zerkms 2015-03-02 07:47:12