开发者

Javascript function syntax needs explanation

return th开发者_JAVA技巧is.foo("abc",function(){
                           //do something
                      });

Can some one tell me what does the above line do? THanks


  1. It grabs a reference to 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.
  2. It (skipping ahead) prepares a new anonymous Function that //does something.
  3. It attempts to invoke a method 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 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.


looks like this.foo() is a function that returns something. so the return value is, what this.foo() returns.


calls an instance method that gets as parameters a string and a function.


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]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜