var PERSON = {
name: 'Jack',
getName: function () {
return this.name;
}
}; 性能
function Person(name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
}
/***************************************************/
var p_obj = new Person('Jim');
/***************************************************/
function test_obj() {
var start = +new Date(),
p = PERSON,
i = 1;
for (; i < 10000000; i++) {
p.getName();
}
alert('Object: ' + ((+new Date()) - start));
}
/***************************************************/
function test_prototype() {
var start = +new Date(),
po = p_obj,
j = 1;
for (; j < 10000000; j++) {
po.getName();
}
alert('prototype: ' + ((+new Date()) - start));
}
/***************************************************/
test_obj();
test_prototype(); this
经过循环调用2种模式下的访问状况(ie7环境): prototype
性能方面: 对象
大多状况下对象字面量模式要比原型快些儿,但也有原型比对象字面量快的时候; get
移植性方面: 原型
很明显,对象字面量模式的移植性更好,由于它能够作为子树便利挂到任何须要的对象上; io