I’m not sure the correct term for this. But I want to write a function that accepts another function and execute it.
For eg.
function test(data, aFunc) {
var newData = data + " Shawn";
aFunc.call(newData);
}
test("hello", function(data){
alert(data);
});
Data is supposed to contain “hello Shawn” string. Help me rewrite this the correct way please.
The first argument of the
callmethod is used to set thethiskeyword (the function context) explicitly, inside the invoked function, e.g.:If you want to invoke a function without caring about the context (the
thiskeyword), you can invoke it directly withoutcall:Note that if you simply invoke a function like
aFunc(newData);or you use thecallorapplymethods with thethisargument set asnullorundefined, thethiskeyword inside the invoked function will refer to the Global object (window).