Inheritance

01

"Inheritance" can be used with Objecs and classes

This shows inefficent code creating duplicate code for each instance of each object created

const me = {
    talk(){
        return 'Talking'
    }
}
const you = {
    talk(){
        return 'Talking'
    }
}
me.talk()
you.talk()

02

Instead of adding methods to individual objects add the method to a class and create instances of that class.

class Person {
    talk(){
        return 'talking'
    }
}
const me = new Person()
const you = new Person()
me.talk()
you.talk()

Now the method "inherits" from the talk() method from the Person class

03

Instead of adding methods to individual objects add the method to a class and create instances of that class.

class Person {
    talk(){
        return 'talking'
    }
}
const me = new Person()
const you = new Person()
me.talk()
you.talk()

Now the method "inherits" from the talk() method from the Person class

05

If you console the me instance, we used to see that any method from the class would reside in __proto__, but this is now depreciated, see next lesson to continue to and see in modern javascript using [[Prototype]]

06

Notice, Methods are not in the instance itself, they are in the [[Protoype]] of it's parent class Person.

class Person{
talk(){
    return 'Talking'
}
}
const me = new Person()
me
                            

07

But we can add attributes to the instance itself

class Person{
talk(){
    return 'Talking'
}
}
const me = new Person()
me.age = 30
me