简体   繁体   中英

Do the parameters passed into an anonymous function in Javascript have special properties?

I've seen snippets of Javascript written like this in various blog posts and I'm having some difficulty wrapping my head around the concept of the properties that parameters have when they are passed into anonymous functions.

For the given example below:

var http = {
    bananas: function(context) {
        var object = {
            saySomething: function (msg) {
                console.log(msg);
            }
        };
        return context(object);
    }
}

http.bananas(function (something) {
    something.saySomething("I like bananas!");
});

" I like bananas! " is returned from this but what I don't understand is why the " context " parameter in the anonymous function for bananas can do this:

return context(object);

Are there special properties that parameters have when they are passed into anonymous functions?

Also how come if I add an additional parameter to the bananas function like this:

bananas: function(context,string){........}

and then try this:

return string(object);

I get an error?

You are dealing with a callback function. Parameter context is expected to be a function. Inside of the bananas function, an object is made and then passed to the callback function context. Context then does it actions.

Please see below.

//Bananas is a method that takes a callback as a parameter
//A callback is a function that is passed to a function as an argument
function bananas(callback){
    //inside of bananas, we use our callback function
    callback('hello');
}

var log = function (string){
    console.log(string);
}
//We call our bananas function, and pass log as the callback
bananas(log);

/*
Idea of how this is executed
bananas(log);
    -> { 
    ->   log(string);    
    -> }
        string is hello
        log('hello');
        ->  {
        ->     console.log('hello')
        ->  }
*/

//The neat thing about callbacks, you can change the function passed
//So, lets change the log function to do something else
log = function(string){
    //since console.log already logs to the console.
    //lets make this log format the string a bit
    var new_string = "Pretty:\n\t" + string;
    console.log(new_string);
}
//Using console.log as a callback
bananas(console.log);

//using log as a callback
bananas(log);

//Your example uses an anonymous function which can make things seem odd
//Does the same as the first log
bananas(function(string){
    console.log(string);
});

//The reason your test did not work
//I can guess that the error was probably
//sometype is not a function
//functionName(param) only works for functions
var string = 'hello';
string('pie'); //string is not a function

string = function(arg){console.log(arg)};

string('pie') //prints pie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM