Extends in javascript

前端 未结 1 762
忘了有多久
忘了有多久 2020-12-29 08:35

Can someone please help me understand this code? Seems too convoluted to me.

var __extends = this.__extends || function (d, b) {
    function __() { this.con         


        
相关标签:
1条回答
  • 2020-12-29 08:41

    So basically

    __extends(MyPageView, _super);
    

    Think in terms ofinheritance in a Object Oriented language. Where a class is extending a Super class or a base class..

    So basically here MyPageView will extend the functionality and implementation of the super class .

    So lets say the base View has method A() and method B() and the current View has method C(), then you current view has access to all the three methods A() , B() and C() in it's view.

    But lets say MyPageView has method B() , defined in it , then the method inside the view will take precedence over the Method B() of Super View

    var __extends = this.__extends || function (d, b) {
        function __() { this.constructor = d; }
        __.prototype = b.prototype;
        d.prototype = new __();
    };
    

    Every function has a magical prototype property.

     var __extends = this.__extends || function (d, b) {
    

    Cheks if that function is available in that context , if not define a function , that takes 2 arguments , The object that is to be extended and the object from which it is extended..

    function __() { this.constructor = d; }
    

    Defining a new function called __ in which that constructor property of the context is bound to object d

     __.prototype = b.prototype;
    

    The prototype property of the Object __ is pointed to the b.prototype chain..

     d.prototype = new __();
    

    The accessing of methods of Super View happens here , where you are setting the prototype property of the Object here..

    So when the new instance is created, if the method is not available , then because of the prototype on the object , it will check the methods in the Super view because it is available on the function __ which is tied to object d

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