Perhaps there is a similar question out there, but I haven't come across it yet and I'm still facing an issue. Here's what I've tried:
function createClass(obj) {
const constructor = obj.constructor;
constructor.prototype = obj;
return constructor;
}
Then I attempted to use it like this:
const Person = createClass({
constructor: function (name) {
this.name = name;
},
voice() {
console.log(`Hello, I'm ${this.name}`);
}
})
This works fine. However, if I want to utilize a constructor in this manner:
const Person = createClass({
constructor(name) {
this.name = name;
},
voice() {
console.log(`Hello, I'm ${this.name}`);
}
})
I believe this is more akin to the native implementation. But when I try this, I receive an error stating 'Person is not a constructor'. What exactly is the distinction between these two approaches? Apologies for such a basic inquiry, I'm just trying to understand this core concept better. Appreciate any assistance.