Why embed the JavaScript class in an anonymous function() call?

前端 未结 6 567
北恋
北恋 2021-02-05 10:31

I was reading about the new JavaScript-like language from Microsoft called TypeScript. In the playground (example section), there is a simple class in TypeScript syntax converte

相关标签:
6条回答
  • 2021-02-05 10:37

    The anonymous function is probably there to prevent name collition with other parts of the code. Think of it this way, inside your anonymous function, you could even declare a variable called "$" to be whatever you want, and at the same time, be using jQuery on other parts of your code without conflict.

    0 讨论(0)
  • 2021-02-05 10:39

    Besides the obvious scoping/closure reasoning. Using an anonymous function that invokes itself immediately pre-loads (interprets) the class definition. This allows any JIT optimizations to be front loaded within the execution. In short, for larger more complex applications it will improve performance.

    0 讨论(0)
  • 2021-02-05 10:42

    This is to allow for private members. In this example, all members are public so your two constructions are equivalent. However, if you want to provide for private members you need to hide them from the calling scope via a closure. Thus if you have a private member like so:

    class Greeter {
        private greeting: string;
        constructor (message: string) {
            this.greeting = message;
        }
        greet() {
            return "Hello, " + this.greeting;
        }
    } 
    

    You would probably get something like this:

    var Greeter = (function () {
        var greeting="";
        function Greeter(message) {
            greeting = message;
        }
        Greeter.prototype.greet = function () {
            return "Hello, " + greeting;
        };
        return Greeter;
    })();
    

    The greeting variable will be available to any function defined inside the anonymous function, but invisible everywhere else.

    0 讨论(0)
  • 2021-02-05 10:51

    The following is called an Immediately Invoked Function Expression:

    (function(){ ... })();
    

    It is used to keep the global scope clean. Though, in this case it isn't necessary since the return value is assigned to a variable Greeter. The only time this pattern is useful is when you want "private" static members.

    E.g.:

    var Greeter = (function () {
        var foo = 'foo', bar = 'bar'; /* only accessible from function's defined
                                         in the local scope ... */
    
        function Greeter(message) {
            this.greeting = message;
        }
        Greeter.prototype.greet = function () {
            return "Hello, " + this.greeting;
        };
        return Greeter;
    })();
    
    0 讨论(0)
  • 2021-02-05 10:55

    The anonymous function / self executing closure is usually used to encapsulate scope so that only the returned value is accessible outside of it. (or anything you attach to other objects, like window)

    0 讨论(0)
  • 2021-02-05 10:59

    The closure is the sole mean to call the constructors with parameters:

    var w = new Greeter("hello")
    

    There are other methods but all complicated and with limitations and drawbacks.

    0 讨论(0)
提交回复
热议问题