Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extends in javascript

Tags:

javascript

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

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


var PageView = (function (_super) {
    "use strict";

    __extends(MyPageView, _super);

 function MyPageView(rootElement, viewModelParameter, calendarWeeksViewModel) {
});


}
like image 824
Nisha Shukla Avatar asked May 16 '13 18:05

Nisha Shukla


People also ask

What does Extends mean in JavaScript?

Definition and Usage The extends keyword is used to create a child class of another class (parent). The child class inherits all the methods from another class. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.

How do you extend a class from another class in JavaScript?

The syntax to extend another class is: class Child extends Parent . Let's create class Rabbit that inherits from Animal : class Rabbit extends Animal { hide() { alert(`${this.name} hides!`); } } let rabbit = new Rabbit("White Rabbit"); rabbit. run(5); // White Rabbit runs with speed 5.

What does Extends mean in typescript?

extends : The child class (which is extended) will inherit all the properties and methods of the class is extends. implements : The class which uses the implements keyword will need to implement all the properties and methods of the class which it implements.

What is the purpose of $this and extends?

Definition and Usage The extends keyword is used to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.


1 Answers

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

like image 168
Sushanth -- Avatar answered Oct 05 '22 16:10

Sushanth --