L
lorlarz
I still have a question regarding the following code,
in a commonly used routine. First, Here's the code in
question:
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).
In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).
This can't be. I must be wrong, Please explain .
in a commonly used routine. First, Here's the code in
question:
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).
In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).
This can't be. I must be wrong, Please explain .