Inheritance
How can inheritance be achieved in JS?
- When a new object is created by using the
new
operator it inherit's the parent functions prototype
Implementation:
function Person() {}
Person.prototype.name = "Default name";
var nert = new Person();
console.log(nert.name); // Default name
// We have overwritten the default value
nert.name = "Nert"
console.log(nert.name) // Nert
Access parent function property
function Person() {}
Person.prototype.name = "Default Name"
var nert = new Person() {}
nert.name = "Nert"
console.log(nert.name) // Nert
// Access parent property
console.log(nert.__proto__.name) // Default Name