[JS]call, apply method
http://dev-tricks.com/trickscall-and-apply-methods-in-javascript/
by tea4two
JavaScript
var o = { x: 15 };
function f(message1, message2) {
alert(message1 + (this.x * this.x) + message2);
}
function g(object, func) {
var args = []; // empty array
// copy all other 'arguments' we want to "pass through"
// check how many args in g();
console.log(arguments.length);
// but I want no.3 and no.4 of args
// which equal to "the value x squared = " and ".Wow!"
for(var i = 2; i < arguments.length; i++) {
args.push(arguments[i]);
}
//this means f.apply(15, [text1, text2])
func.apply(object, args);
//apply's first arguments(object) is invisible
//defaultly passed to function
}
g(o, f, "The value of x squared = ", ". Wow!");