In my current JavaScript class structure, the DataService is defined as follows:
// data.service.ts
export class DataService {
public url = environment.url;
constructor(
private uri: string,
private httpClient: HttpClient,
) { }
getAll() {}
getOne(id: number) {}
create(data: any) {}
// etc...
}
Following this, there is a general data model that utilizes the methods of DataService to interact with the server:
// Model.model.ts
import './data.service';
export class Model extends DataService {
all() {}
get() {
// parse and perform basic validation on the DataService.getOne() JSON result
}
// etc...
}
Lastly, I have created a specific data model based on Model.model.ts named User.model.ts:
// User.model.ts
import './Model.model.ts';
export class User extends Model {
id: number;
name: string;
email: string;
init() {
// implement specific validation on Model.get() result
}
}
When utilizing the User class in my code, it allows for direct calling of the DataService's getAll() function. However, this can lead to bypassing the built-in validations.
I am interested in blocking method inheritance within a class. Is there a way to achieve this in JavaScript similar to PHP's static method functionality?
I envision a scenario where:
const dataService = new DataService();
dataService.getAll(); // returns void
const model = new Model();
model.getAll(); // returns undefined
model.all(); // returns void
const user = new User();
user.getAll(); // returns undefined
user.all(); // returns void
Is there a workaround or approach to restrict method inheritance like this?