closures - Do the parameters passed into an anonymous function in Javascript have special properties? -
I have seen the snippets of javascript written in different blog posts like this and I have some problem that is the concept of properties, when They are passed in anonymous works.
For the example given below:
var http = {banana: function (context) {var object = {say something: function (msg) {console.log (Msg); }}; Return reference (object); }} Http. Ban (function (some) {something.saySomething ("I love bananas!");});
" I like bananas!
" It's back from but what I do not understand is why " context
"Anonymous function can do this for parameter bananas:
references (object);
What are the special properties of the parameter to be passed in the unnamed properties?
How does this happen even when I add an extra parameter to the banana function: / P>
banana: function (reference, string) {........}
Try again:
return string (object);
I get an error?
You expect a callback function parameter reference to be a function inside the banana function, create an object And then the callback function goes into context.
// ban is a method that takes as a callback parameter / / a callback is a function that has been passed to create a logic function for a function (callback) { // Inside Kerala, we use our callback function callback ('hello'); } Var log = function (string) {console.log (string); } // We call our banana function, and log in as a callback; / * Idea of how bananas are executed (logs); - & gt; {- & gt; Log in (string); - & gt; } String hello log ('hello'); - & gt; {- & gt; Console.log ('hello') - & gt; } * / // The clean thing about the callback, you can pass the function // then, change the log function from something else to log = function (string) {// after the change from the console. Log already logs in console // this log format creates the string a bit var new_string = "beautiful: \ n \ t" + string; Console.log (new_string); } // Use console.log as a callback banana (console.log); // Using the log as Callback Ban (Log); // Your example uses an anonymous function which can look strange to / does / what is the same as logged (function) {console.log (string);}); // Your test was not working due to / / I can guess that the error is probably not the // sometype function // functionName (param) only works for var string = 'hello'; String ('pie'); // string is not a function string = function (arg) {console.log (arg)}; String ('pie') // print pie
Comments
Post a Comment