Web Analytics Made Easy -
StatCounter function declaration understanding - CodingForum

Announcement

Collapse
No announcement yet.

function declaration understanding

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Angular function declaration understanding

    Hi all,

    I would like to ask a question about javascript( I think it's prototype based) function declarations.
    While using third party js module(http://tags.expo9.exponential.com/ta...tv/BTF/tags.js), I am stuck with a question.

    Let me list down the function declaration:

    expo9_ad = (function() {
    var version = "1.20";
    var displayAdVersion = "0.3";

    function expo9_ad() {
    var t = this;
    t.host = "a.tribalfusion.com";
    ...
    }

    expo9_ad.prototype.showAd = function () {
    var t = this;
    t.url = "";
    ...
    }
    ...
    ...
    return expo9_ad;
    })();

    It seems to me like this :
    expo9_ad = (function() {return expo9_ad;})();
    But I can't understand how it works.

    Any help would be appreciate.
    Thank you,
    Christopher

  • #2
    This construct
    Code:
    (function() {
    })();
    is actually a self-calling anonymous function. It is mostly used to separate the script scope from the global namespace (to prevent cluttering of the global namespace).

    In this case a new class is created inside the scope and then returned to the global scope where it is then available with the same name. Actually I don't really see the advantage of using a self-calling anonymous function here.

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎