Understanding Javascript generated by Typescript compiler

后端 未结 2 1244
孤城傲影
孤城傲影 2021-01-23 00:53

if i type this simple typescript code:

class Point2D
{
    constructor(public x: number, public y: number)
    {
    }
} 

and look at the gener

2条回答
  •  抹茶落季
    2021-01-23 01:38

    What you see is The design pattern - Module. See more here:

    • JavaScript Module Pattern: In-Depth (written by ben cherry)

    a cite:

    Module Export

    Sometimes you don’t just want to use globals, but you want to declare them. We can easily do this by exporting them, using the anonymous function’s return value. Doing so will complete the basic module pattern, so here’s a complete example:

    var MODULE = (function () {
        var my = {},
            privateVariable = 1;
    
        function privateMethod() {
            // ...
        }
    
        my.moduleProperty = 1;
        my.moduleMethod = function () {
            // ...
        };
    
        return my;
    }());
    

    Notice that we’ve declared a global module named MODULE, with two public properties: a method named MODULE.moduleMethod and a variable named MODULE.moduleProperty. In addition, it maintains private internal state using the closure of the anonymous function. Also, we can easily import needed globals, using the pattern we learned above.

    Another really fundamental resource is:

    • Learning JavaScript Design Patterns (A book by Addy Osmani)

    a cite:

    The Module Pattern

    In JavaScript, the Module pattern is used to further emulate the concept of classes in such a way that we're able to include both public/private methods and variables inside a single object, thus shielding particular parts from the global scope. What this results in is a reduction in the likelihood of our function names conflicting with other functions defined in additional scripts on the page...

    see more in the links

提交回复
热议问题