Hi I am very new to JS.
return this.foo("abc",function(){ //do something }); Can someone tell me what does the above line do?
Hi I am very new to JS.
return this.foo("abc",function(){ //do something }); Can someone tell me what does the above line do?
this, which might be the DOM Window, a DOM element, or any other JavaScript object depending on how and where the above code is being run.Function that //does something.foo on object this, passing in two parameters "abc" and said anonymous Function.Very often when you see code that passes along an anonymous function (e.g. function(){ ... }), it is in fact holding on to that function in order to execute it not right away but at some later point in time, such as in response to a click event or a timer.
It will return (yeah, like it is in English) the returned result of a function this.foo(...) (in the most simple form, ithe function this.foo(...) return "something" then the code will return "something"). The function this.foo("abc", function(){...}); is itself a function which receives 2 arguments: A string "abc" and a function(). The function this.foo will do something and return "something" to return by the main function. [x]
It calls the function referenced by this.foo and passes two parameters: The string "abc" and an anonymous function function(){ //do something}. It then returns the result.
It is equivalent to:
var a = "abc"; var b = function(){ //do something }; return this.foo(a, b); Functions are first class objects in JS so you can pass them around like any other value.
I recommend to have a look at the MDC JavaScript guide.