I don't understand Crockford on [removed] The Way Forward

后端 未结 4 1103
北恋
北恋 2021-01-30 05:45

In a lecture called "The Way Forward", Douglass Crockford shares that he no longer uses "new" in his JavaScript, and is weaning himself off of "this&quo

4条回答
  •  梦毁少年i
    2021-01-30 06:11

    The purpose of the assignment is to make the method "public". Without this assignment, the method is "private" to the "class".

    Maybe i can try to make the code more understandable :

    function constructor(init) {
        // Call the mother constructor. Or just do that = init.
        var that = other_constructor(init);
    
        // Private members
        var member1, member2;
    
        // Methods
        var method1 = function() {
            do_stuff();
        };
    
        var method2 = function() {
            do_stuff();
        };
    
        // Make some method public.
        that.method1 = method1;
    
        return that;
    }
    

    Then in you code, you can use your constructor like this :

    var obj = constructor(other_obj);
    obj.method1();
    

提交回复
热议问题