最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

design patterns - Global import in javascript - Stack Overflow

programmeradmin1浏览0评论

Can somebody explain to me what this does in javascript?

 (function (x,y){}(x,y));

or this

  // global import
  (function ($, YAHOO) {
         // now have access to globals jQuery (as $) and YAHOO in this code
  }(jQuery, YAHOO));

I have never seen something similar in other languages like java or c++

Can somebody explain to me what this does in javascript?

 (function (x,y){}(x,y));

or this

  // global import
  (function ($, YAHOO) {
         // now have access to globals jQuery (as $) and YAHOO in this code
  }(jQuery, YAHOO));

I have never seen something similar in other languages like java or c++

Share Improve this question asked Jul 1, 2012 at 21:36 VladVlad 2,7737 gold badges52 silver badges106 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

function(x, y) { } is an anonoymous function.

(x, y) calls that function, passing x and y as parameters.

The purpose of the second code block is to make jQuery available as $ (without defining $ globally) and importing YAHOO in the local scope to make variable lookups a bit faster.

Besides that, it creates a new scope so any variables defined with var inside will not be global.

The first code block is often used in this way to create new variables, e.g. when you are in a loop and need to create a variable with the current i value for a callback:

for(var i = 0; i < 10; i++) {
    (function(i) {
        setTimeout(function() {
            alert("number " + i);
        }, 1000 * i);
    })(i);
}

Without that new scope you'd have the same i everytime and thus alert "number 10" 10 times.

The first one calls an anonymous function, which takes 2 arguments and doesn't do anything, with x and y as argument.

The second does the same thing. It uses $ as the first argument name, and passes jQuery as the argument value. It's thus possible, inside this function to use $ to refer to the jQuery object. It's a way to alias variables to another name inside a function.

C# has a similar concept, called a Lambda expression.

public static Func<int> Foo()
{
    int a = 1,
        b = 2;

    return () => a + b;
}

It's a generic, first-class function (by first class function, it is meant that the function itself is returned, not the result thereof) that is declared anonymously.

In this case, I also show you what's called a closure - the returned method encloses the 'local' variables a and b, allowing them to persist when Foo goes out of scope. In many languages, anonymous methods implement closures.

Anonymous methods are really cool too - you can perform behavior injection, as in this benchmark method:

public static TimeSpan BenchmarkMe(Action timeThis)
{
    Stopwatch sw = Stopwatch.StartNew()
    timeThis();
    sw.Stop;

    return sw.Elapsed;
}

You can pass an anonymous function that returns a TimeSpan into this method, which will perform a benchmark for a single pass against it. Nifty, eh?

发布评论

评论列表(0)

  1. 暂无评论