Hello,
I'm trying to do something with function.prototype.call
to ensure a dynamic inheritance.
Here's a basic example what I'm trying to do:
class Person {
constructor(name, test) {
this.name = name;
this.test = test;
}
}
class Police {
constructor(name, badge, ...manyArgs) {
//Attempt 1:
Person.call(this, name, 'hello world');
//I also tried:
Person.constructor.call(this, name, 'hello world');
console.log(this.test); //This should print a log message of 'hello world'
}
}
The first attempt doesn't work because a class is not a function, and only functions have the call method in their prototype.
The second attempt doesn't give an error, but just doesn't inherit the test value set in Person
.
Something that does work is if I would change the Person
class to:
function Person(name, test) {
this.name = name;
this.test = test;
}
But unfortunately I don't have the luxery to change the class' code that I'm trying to inherit like this.
I've searched online a lot, but couldn't find why the call
function wouldn't work for class
-based classes. It's confusing for me because you can easily rewrite class
-based classes to function
-based classes.
Does anyone have an idea of how to use the prototype.call
method to inherit a class?