Saltar al contenido

superclase en ejemplo de código javascript

Haz todo lo posible por comprender el código bien previamente a usarlo a tu trabajo y si tdeseas aportar algo puedes compartirlo con nosotros.

Ejemplo 1: super en javascirpt

classRectangleconstructor(height, width)this.name='Rectangle';this.height= height;this.width= width;sayName()console.log('Hi, I am a ',this.name+'.');getarea()returnthis.height*this.width;setarea(value)this._area= value;classSquareextendsRectangleconstructor(length)this.height;// ReferenceError, super needs to be called first!// Here, it calls the parent class's constructor with lengths// provided for the Rectangle's width and heightsuper(length, length);// Note: In derived classes, super() must be called before you// can use 'this'. Leaving this out will cause a reference error.this.name='Square';

Ejemplo 2: método de llamada de clase extendida de super en javascript

// this is how we call method from a super class(i.e MAIN CLASS) to extended classclassPersonconstructor(name, age)this.name= name;this.age= age;greet()console.log(`Hi, all my name is $this.name`);classEmployeeextendsPersonconstructor(name, age, employer)super(name, age);// NOTE : super must call before accessing (this)this.employer= employer;greeting()super.greet();// this is how we callconsole.log(`I'm working at $this.employer`);letSteve=newEmployee('Xman',25,'Skynet');Steve;Steve.greeting();// check by consoling MATE

Ejemplo 3: javascript super

classParentconstructor()method()classChildextendsParentconstructor()super()// Parent.constructorsuper.method()// Parent.method

Si estás de acuerdo, puedes dejar una división acerca de qué te ha parecido esta división.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *