FANDOM


Sample codeEdit

Here myFunction is exposed to the global namespace.

function myFunction() {
    // do stuff
}
 
// call myFunction on the $(document).ready() event (see jQuery API)
$(myFunction);

Using anonymous functionEdit

An anonymous function is a function without an identifier (name). This is one way to make sure that myFunction isn't exposed to the global namespace.

// call an anonymous function on the $(document).ready() event (see jQuery API)
$(function () {
    // do stuff
});

Hiding your code inside a closureEdit

Sometimes, you might have more than 1 function. You can hide all your code in a closure to prevent it from being exposed to the global namespace.

// this self-executing anonymous function creates a closure
// so that myFunction isn't exposed to the global namespace
(function () {
    function myFunction() {
        // do stuff
    }
 
    function anotherFunction() {
 
    }
 
    // call myFunction on the $(document).ready() event (see jQuery API)
    $(myFunction);
}());

Standard MediaWiki coding conventionEdit

...is very similar to the above: http://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.