I got a script like this,
var first_object = {
num: 42
};
function multiply(mult) {
return this.num * mult;
}
//Bind part
Function.prototype.bind = function (obj) {
var method = this,
temp = function () {
return method.apply(obj, arguments);
};
return temp;
}
var first_multiply = multiply.bind(first_object);
first_multiply(5); // returns 42 * 5
alert(second_multiply(5));
My question is, in the Bind part, cause what i really need is just the result from
method.apply(obj, arguments);
I just make the Bind part code like
Function.prototype.bind = function (obj) {
var method = this,
return method.apply(obj, arguments);
}
But this change causes error.. and I don't know why. What I guess is the reason I need a var temp to hold the function is, js doesn't know what will be returned by method.apply(obj, argument), which cannot be returned, but it knows temp is a var, which can be returned.
I don't know if my guess is correct and the real reason is.
EDIT This is the error looks like
0x800a138a - JavaScript runtime error: Function expected