文字对象和函数构造函数 [英] Literal object and function constructor
本文介绍了文字对象和函数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 http://jsperf.com/literal-obj-vs上进行了此测试-function-obj 和 Literal 在FF6,Opera 10,IE8上获胜,但Chrome 13.0.782.112上的 Function 方法更快,所以哪一个更好方法来使用?
I took this test on http://jsperf.com/literal-obj-vs-function-obj and Literal wins on FF6, Opera 10, IE8, but the Function method faster on Chrome 13.0.782.112, so which one is a better method to use?
var A = {
aa : function(){
var i, j=[];
var arr = ['Literal', 'Function'];
for (i = 0; i < arr.length; i++){
j[i] = arr[i];
}
return j[0];
}
};
var A1 = A;
var A2 = A1;
A1.foo = ' Test';
alert(A1.aa() + A2.foo);
//Function test
function B(){
this.bb = function(){
var i, j=[];
var arr = ['Literal', 'Function'];
for (i = 0; i < arr.length; i++){
j[i] = arr[i];
}
return j[1];
}
}
var B1 = new B();
var B2 = new B();
B.prototype.foo = ' Test';
alert(B1.bb() + B2.foo);
推荐答案
发现是混合:
To tell you the truth, the best that I have found is a mix:
function C() {
var i, j = [],
foo;
return {
bb: function() {
var arr = ['Literal', 'Function'];
for (i = 0; i < arr.length; i++) {
j[i] = arr[i];
}
return j[1];
},
setFoo: function(val) {
foo = val;
},
getFoo: function() {
return foo;
}
}
}
var C1 = C();
var C2 = C();
C2.setFoo(' Test');
console.log(C1.bb(), C2.getFoo());
这篇关于文字对象和函数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文