所以我写了这些测试,看看使用原型会有多快...
function User() {
return {
name: "Dave",
setName: function(n) {
this.name = n;
},
getName: function() {
return this.name;
}
};
}
function UserPrototype() {
if (!(this instanceof UserPrototype)) return new UserPrototype();
this.name = "Dave";
}
UserPrototype.prototype.getName = function() {
return this.name;
};
UserPrototype.prototype.setName = function(n) {
this.name = n;
};
function setName(obj,name)
{
obj.name = name;
}
function getName(obj)
{
return obj.name;
}
//Test 1
var c = 10000000;
var tstart = 0;
var tend = 0;
tstart …Run Code Online (Sandbox Code Playgroud)