Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
Herencia clásica con Object.create() y... ¿mecanografiado?

Estaba mirando el documento de MDN en Object.create() para ver cómo funciona mientras miraba el código de alguien. Pero esto es lo que me pregunto ahora, como novato mecanografiado.

¿Cómo puedo hacer que su ejemplo funcione con mecanografiado sin usar una clase?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#classical_inheritance_with_object.create

En un archivo ts, no puedo encontrar una forma de evitar este error sin usar clases js: 'this' implicitly has type 'any' because it does not have a type annotation.

EDITAR:

Me acerco si hago algo como esto:

 function Shape(this: any) { this.x = 0; this.y = 0; } // superclass method Shape.prototype.move = function(x: any, y: any) { this.x += x; this.y += y; console.info('Shape moved.'); }; // Rectangle - subclass function Rectangle(this: any) { Shape.call(this); // call super constructor. } // subclass extends superclass Rectangle.prototype = Object.create(Shape.prototype); //If you don't set Rectangle.prototype.constructor to Rectangle, //it will take the prototype.constructor of Shape (parent). //To avoid that, we set the prototype.constructor to Rectangle (child). Rectangle.prototype.constructor = Rectangle; const rect = new Rectangle(); console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true console.log('Is rect an instance of Shape?', rect instanceof Shape); // true rect.move(1, 1); // Outputs, 'Shape moved.'
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Oh... esto parece hacerlo...

 function Shape(this: any) { this.x = 0; this.y = 0; } // superclass method Shape.prototype.move = function(x: any, y: any) { this.x += x; this.y += y; console.log('Shape moved.'); }; // Rectangle - subclass function Rectangle(this: any) { Shape.call(this); // call super constructor. } // subclass extends superclass Rectangle.prototype = Object.create(Shape.prototype); //If you don't set Rectangle.prototype.constructor to Rectangle, //it will take the prototype.constructor of Shape (parent). //To avoid that, we set the prototype.constructor to Rectangle (child). Rectangle.prototype.constructor = Rectangle; const rect = new (Rectangle as any)(); console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true console.log('Is rect an instance of Shape?', rect instanceof Shape); // true rect.move(1, 1); // Outputs, 'Shape moved.'
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda