javascript - How does 'function1(data, function2)' work? -
how these code work ?
function output(a) { console.log( "the function filter return " + + "!"); } x = function(data, fun) { = data; fun(a); }; theinput = " text input "; x(theinput, output);//the function filter return text input !
i wrote mysels , , works fine . don't understand how 'function1(data, function2)' run .
what
x
?
it variable holds reference function 2 parameters, data
, fun
.
whats doing here?
x(theinput, output);
you call function passing string , function. yes, functions in javascript can treated other object. actually, objects. can stored variables (store reference them), can passed arguments function etc.
what happening inside body of function stored in x ?
initially, assign data
variable called a
, pass argument function output
. function stored in output
called.
if there 1 takeaway code snippet fact passed function argument function. important in javascript , associated nature of functions in javascript.
according mdn:
in javascript, functions first-class objects, i.e. objects , can manipulated , passed around other object. specifically, function objects.
Comments
Post a Comment