Tengo un constructor padre como este:
let list = []; class Pictures { constructor(price, title) { this.price = price; this.title = title; list.push(this) } updatePrice(price_increase) { this.price = this.price * price_increase / 100 + this.price return this.price } }También tengo dos clases secundarias que heredan de la clase Pictures(Parent)
class Photograph extends Pictures { constructor(photographer, camera, aperture, contrast, price, title) { super(price, title); this.price = price; this.title = this.title; this.photographer = photographer; this.camera = camera; this.aperture = aperture; this.contrast = contrast; } alterContrast(new_contrast) { this.contrast = new_contrast; } toString() { return `Photographer: ${this.photographer}, Camera: ${this.camera}, Aperture: ${this.aperture}, Contrast: ${this.contrast}`; } }Y:
class Painting extends Pictures { constructor(artist, type, owner, title, price) { super(price, title); this.price = price; this.title = title; this.artist = artist; this.type = type; this.owner = owner; } printProvenance() { } toString() { return `Artist: ${this.artist}, Type: ${this.type}, Owner: ${this.owner}`; } }Y estas son las instancias de las clases.
let photo_one = new Photograph('Kyle', 'Nikon', 32, 21, 30, 'Sunset') let photo_two = new Photograph("Maya", "Sony XPR", 100, 23, 100.00, "Festival of Color"); Empujé todas las instancias a la matriz de "lista" en el constructor principal. ¿Es posible recorrer la matriz y usar el método updatePrice en la clase Pictures para actualizar el valor del precio de todos los objetos?
Sí, puedes, solo usa .map , .forEach o for loop
list.map((item) => { item.updatePrice(1000) })Pero puedo recomendar almacenar 'lista' dentro de la clase.