我在多个地方读到,谨慎使用arguments
对象是明智的,这是可以接受的...
var i = arguments.length, args = new Array(i);
while (i--) args[i] = arguments[i];
但是,这样做是否可行呢?...
function makeArray (l) {
var i = l.length, array = new Array(i);
while (i--) array[i] = l[i];
return array;
};
//...
//EDIT: put the function on an object to better represent the actual case
var o = {};
o.f = function (callback) {
var args = makeArray (arguments);
callback.apply(args[0] = this, args);
};
什么是“泄漏参数”,它如何影响优化?
我专注于V8,但我认为它也适用于其他编译器?
为了证明它的有效性...
function makeArray (l) {
var i = l.length, array = new Array(i);
while (i--) array[i] = l[i];
return array;
};
//...
//EDIT: put the function on an object to better represent the actual case
var o = {};
o.f = function (callback) {
var args = makeArray (arguments);
callback.apply(args[0] = this, args);
};
o.m = "Hello, ";
function test(f, n) {
alert(this.m + " " + n)
}
o.f(test, "it works...")
var o={f:function(){ [code] }};
代替var o={}; o.f=function(){ [code] };
。 - Ismael Miguel