🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Marek S Sun, 21 Jan 2018 22:56
Javascript ES6 - bezużyteczne dziedziczenie klas?
#408623
Author: Marek S
Date: Sun, 21 Jan 2018 22:56
41 lines
1683 bytes
Mamy następującą sytuację. Dwóch programistów napisało dwie klasy. Każdy 
z nich wykorzystał zmienną "a" w swoich klasach. W JS nie ma zmiennych 
prywatnych więc skutek tego jest taki, że jeśli teraz programista klasy 
bClass będzie chciał dziedziczyć aClass to szlag trafi całą 
funkcjonalność. Obie klasy do zupełnie innych celów wykorzystywały 
zmienną "a". Jedna klasa będzie grzebała w zmiennych drugiej. Da się nad 
tym zapanować?


"use strict";

class aClass {
	readFromA() {
		console.log(this.a);
	}
	
	
	constructor() {
		this.a = 5;
	}
}

class bClass extends aClass {
	readFromB() {
		console.log(this.a);
	}
	
	
	constructor() {
		super();
		this.a=10;
	}
}

let bc=new bClass();
bc.readFromA(); //pokaże 10
bc.readFromB(); //pokaże 10

-- 
Pozdrawiam,
Marek
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads