English 中文(简体)
What is the (function($)) for in MooTools Javascript
原标题:

I was reading a post by David Walsh on creating a sprite menu with MooTools. I am fairly new to MooTools, and I did not understand the way he opened his script. In particular, what is

(function($){
..
})(document.id)

Is it a type of JS closure?

Here is the full script if you don t want to follow the link:

(function($) {
    window.addEvent( domready ,function() {
    $( nav ).getElements( li ).each(function(li) {
        //settings
        var link = li.getFirst( a );
        //fix background image
        if(!li.hasClass( current )) {
            link.setStyle( background-image , none );
        }
        //utility div
        var div = new Element( div ,{
             class :  nav-  + li.get( id ),
            opacity: 0
        }).inject(li);
        //background imagery
        li.addEvents({
            mouseenter: function() {
                div.fade( in );
            },
            mouseleave: function() {
                div.fade( out );
            },
            mousedown: function() {
                div.addClass( nav-  + li.get( id ) +  -click );
            },
            mouseup: function() {
                div.removeClass( nav-  + li.get( id ) +  -click );
            }
        });
    });
});
})(document.id);
最佳回答
(function($){
..
})(document.id)

If you put a function inside (), JavaScript will automatically execute that function. By adding (document.id) he is also passing a parameter to that function. For instance..

(function(message){
    alert(message);
})(  Hello World!  );

Would alert Hello World!

问题回答

It assigns document.id (a function created by MooTools) to a local variable (named "$") of the anonymous function s scope.

It s an auto-executing closed function using Mootools "dollar safe mode".





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签