EDIT: I've added updated screenshots (at the end), for further clarification.
I'm attempting to use high order functions to compose subclasses/mixins but noticed I can only access the properties of the very first class I extend and can only access the properties of any subsequent classes after I've called the class. Here is a contrived example of what I mean:
These are functions that will apply subclasses to the parent class:
export const middleNameClass = middlename => BaseClass => {
return class extends BaseClass {
constructor(args) {
super(args);
this.middlename = middlename;
}
};
};
export const lastNameClass = lastname => BaseClass => {
return class extends BaseClass {
constructor(args) {
super(args);
this.lastname = lastname;
}
};
};
Here is firstNameClass
, which will be extended directly by the parent class, Person
:
class firstNameClass {
constructor(args) {
this.firstname = args;
}
}
This is Person
, which extends firstNameClass
:
class Person extends firstNameClass {
constructor(args) {
super(args);
this.getNames = this.getNames.bind(this);
// I'm using to log class properties to the console
this.getNames();
}
getNames() {
console.log("this inside getNames(): ", this);
console.log("firstnames inside getNames(): ", this.firstname);
console.log("middlenames inside getNames(): ", this.middlename);
console.log("lastnames inside getNames(): ", this.lastname);
}
}
and finally, here is where I apply my higher order functions and create my class:
const enhancedClass = compose(
middleNameClass("Allan"),
lastNameClass("Poe")
)(Person);
const namedPerson = new enhancedClass("Edgar");
However, I see the following when I check my console.log:
this.firstnames inside getNames(): Edgar
this.middlenames inside getNames(): undefined
this.lastnames inside getNames(): undefined
Could someone explain what I'm doing wrong?
EDIT:
Here are the contents of my Person
class:
and here is what is output to the console, after I create the class:
At
new enhancedClass('Edgar')
, this happens:lastNameClass
's constructor callssuper
middleNameClass
's constructor callssuper
Person
's constructor callssuper
firstNameClass
doesthis.firstName = 'Edgar'
Person
, which callsgetNames
middleNameClass
, which doesthis.middleName = 'Allan'
lastNameClass
, which doesthis.lastName = 'Poe'
Calling
getNames
afterwards should work. Same thing would've happened if you usedextend
every time.This isn't an answer to your question but maybe it's a solution to your problem
JavaScript doesn't have multiple inheritance but luckily for you functional programming has nothing to do JavaScript's class system, object prototypes, or other object-oriented concepts. Functional programming is all about functions!
We begin writing our inheritable modules with some functions
We haven't defined
field
yet, but don't worry. Let's look at a somewhat more involved module nextOK, so now we can see how some modules can be composed of other modules. Before we look at
inherit
andmethod
, let's see how we'd use our moduleSo maybe you're annoyed that I keep making stuff up in each new code paste, but this is a very powerful practice called wishful thinking
Exercising this practice, we wished up this imaginary object system based on what we need it to do - not based on what JavaScript's object system is capable of.
Of course we expect that using our
Person
will be straightforwardAnd here's the dependencies: What I want you to see here is that no
class
or eventhis
is used. So even if JavaScript didn't have a native object system, this demonstrates you could make your ownFull program demonstration
Our Person class can obviously define its own fields and methods as well
Get creative and invent any other features you want
overrideMethod
that gives you the ability to define a new method with the same name, but still give the caller access to both methodsprivateMethod
orclassMethod
helpersfield
could be enhanced to emit events when values changedfield
could be changed so that values cannot be set, wheremutableField
could be used for fields that can changeWrite it how you want it then make your wishes come true. Any limitations are you own.