有没有性能或功能上的区别,在JavaScript构造函数中返回JavaScript对象字面量,与仅使用“this.XYZ”设置属性?例如:
function PersonA(fname, lname) {
this.fname = fname;
this.lname = lname;
}
function PersonB(fname, lname) {
return {
"fname": fname,
"lname": lname
};
}
两者都表现得很合适:
PersonA.prototype.fullName = function() { return this.fname + " " + this.lname; };
PersonB.prototype.fullName = function() { return this.fname + " " + this.lname; };
var pA = new PersonA("Bob", "Smith");
var pB = new PersonB("James", "Smith");
alert(pA.fullName());
alert(pB.fullName());
有没有任何原因使一种更可取,或只是个人口味而已?如果是口味的话,是否有一种更为标准?