Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Funciones Javascript, herencia de múltiples prototipos

Tengo una tarea para implementar un diagrama usando prototipos y constructores de javascript. Por ahora tengo que implementar la herencia múltiple usando prototipos. Sé cómo implementar la herencia única y estoy atascado en la herencia de múltiples prototipos.

Diagrama UML

Esta pregunta se centra en los objetos Event y DataType heredados de WeatherData .

 import { DataType, Event } from "./../common/EventData.mjs" export function WeatherData(value, { time, place }, { type, unit, isInternational }) { Event.call(time, place) DataType.call(type, unit, isInternational) this.value = value } WeatherData.setPrototypeOf(WeatherData.prototype, Event.prototype) WeatherData.setPrototypeOf(WeatherData.prototype, DataType.prototype) WeatherData.prototype.getValue = function () { return this.value }

No he probado el código, pero estoy seguro de que está mal porque el segundo .setPrototypeOf() sobrescribe la primera función, lo que significa que el prototipo de WeatherData será DataType .

He buscado en Internet y no pude encontrar respuesta para esto, tal vez porque esta metodología está obsoleta.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Uno podría darle al código del OP un intento de refactorización del código de pegamento de herencia múltiple como este ...

 import { DataType, Event } from "./../common/EventData.mjs" function WeatherData( value, { time, place }, { type, unit, isInternational } ) { // - close to an instance level super call but done twice. // // - technically applying two function based mixins. Event.call(this, time, place); DataType.call(this, type, unit, isInternational) this.value = value } // - prototype level kind of multiple superclass extension. // // - technically mixed-in prototype objects via // `Object.assign` WeatherData.prototype = Object.assign( // ... aggregate a compound prototype. {}, Event.prototype, DataType.prototype, ); // prevent latest mixed-in super-constructor, here // `DataType`, from being the sub-classed constructor. WeatherData.prototype.constructor = WeatherData; WeatherData.prototype.getValue = function () { return this.value; } export/* default*/ WeatherData;

La implementación del constructor anterior cubre la parte de mezcla a nivel de instancia/objeto. El código que agrega y asigna un compuesto prototipo de otros dos objetos prototipo es lo más cercano que se puede llegar a la herencia múltiple con lo que está disponible en JS.

Pero el diseño del código anterior también tiene fallas en el sentido de que dicho prototipo compuesto pierde cualquier vínculo adicional con cualquiera de las cadenas de prototipos posiblemente disponibles de Event o DataType .

Por lo tanto, desde una perspectiva de modelado, era mejor si la base de código disponible se proporcionara de manera que se pudiera permitir que WeatherData heredara de DataType , mientras que una implementación agnóstica de prototipo de Event podría aplicarse adicionalmente como una combinación basada en funciones.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!