Let's consider I have the following code
/*...*/
var _fun = fun;
fun = function() {
/*...*/
_fun.apply(this, arguments);
}
I have just lost the .length
data on _fun
because I tried to wrap it with some interception logic.
The following doesn't work
var f = function(a,b) { };
console.log(f.length); // 2
f.length = 4;
console.log(f.length); // 2
The annotated ES5.1 specification states that .length
is defined as follows
Object.defineProperty(fun, "length", {
value: /*...*/,
writable: false,
configurable: false,
enumerable: false
}
Given that the logic inside fun
requires .length
to be accurate, how can I intercept and overwrite this function without destroying the .length
data?
I have a feeling I will need to use eval
and the dodgy Function.prototype.toString
to construct a new string with the same number of arguments. I want to avoid this.
length
to be properly set? You may want have a look at developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…, many libraries also simulate this behavior. – Supreme