JSFiddle - React, Tailwind, and code Playground
JavaScript
var Person = function(name, email) {
this.name = name;
this.email = email;
// 1 possible way to define a "composite property"
Object.defineProperty(this, "nameWithEmail", {
get: function() {
return this.name + " <" + this.email + ">";
}
});
// I think a simpler way might be to just use a function (see `toString`)
};
// things defined under prototype will be "shared/inheried" by all classes,
// as opposed to name and email for example
Person.prototype.sayHi = function() {
console.log("Hello, my name is " + this.toString());
};
Person.prototype.toString = function() {
return this.name + " <" + this.email + ">";
};
// we define the object properties in the constructor function
var Employee = function(name, email, position) {
Person.call(this, name, email); // calling person constructor: something like super() in other languages
this.position = position;
};
// extend the Employee class with Person
//Employee.prototype = Object.create(Person.prototype);
_.extend(Employee.prototype, Person.prototype);
_.extend(Employee.prototype, {
doSomething: function() {
return "hi ...";
}
});
// override some functions
Employee.prototype.sayHi = function() {
Person.prototype.sayHi.call(this);
console.log("I am a " + this.position);
};
x = new Employee("j", "[email protected]", "developer");
console.log(Employee.prototype);