Polymorphism in C++ is implemented with Virtual Method Tables
Polymorphism in Python is implemented with modified DFS
Today we're going to look at OOP in JavaScript.
Modern JavaScript OOP syntax has the class keyword, but today we're going to be using older javascript (pre-2015) syntax to help us understand how class is implemented under-the-hood.
a = {sdf: 3, noodle: 999} // Objects look like python-dicts
myobj = {sumVal: 3, sumMethod: function() {console.log("Hello")}}
myobj.sumVal
myobj.sumMethod() // => HelloSo we don't have classes in Javascript, instead we uses functions as classes.
function Dog() {}
mydog = new Dog() // this returns an object of type Dog
                  // we're using that function as a constructorLet's set up some fields for Dog
function Dog() {
    this.legs = 4
}
mydog = new Dog() // => Dogs {legs: 4}What happens if we call Dog as a regular function and not as a constructor?
Dog() // => undefinedConstructors are not supposed to return vales.
function Dog() {
    this.legs = 4
    this.bark = function() {
        return "Woof"
    }
}
mydog = new Dog()
mydog.bark() // => "Woof"So this works, but it's not great because every time we call the constructor we create a new function. In python, when we create a methods, they are stored in one class object.
function Dog() {
    this.legs = 4
}
Dog.prototype.bark = function() {
    this.legs = this.legs + 1
    return "Woof"
}
mydog = new Dog() 
mydog.bark()An object that lives inside every JS class that contains the constructor method and all other defined methods.
Similar to the prototype object, but different in some very obnoxious ways.
__proto__ lives in the instance of a class.
The __proto__  object is a field of every JS object that contains all the methods that are in the class' prototype.
Currently, if we do Dog.__proto__.__proto__ we'll get Object's proto
What if we wanted Dog.__proto__.__proto__ to be equal to the proto of a class Animal?
function Animal(numOfLegs){
    this.legs = numOfLegs;
    this.isdead = false;
}
Animal.prototype.kill = function(){
    this.isdead = true;
}
/////////
function Bird(){
}
Bird.prototype = new Animal(2); // This is how we inherit from Animal
Bird.prototype.fly = function(){
    if (this.isdead)
        console.log("error: flying disabled")
    else
        console.log("ok: flap flap")
}When I access a field or method, JS first looks in the object definition. Then, it searches. First in the proto of the current object and then, the proto of the proto and so on.
class Show a where
    show :: a -> String
instance Show Whatever where
    show _ = "I am a Whatever instance"
data Something = Something
instance Show Something where
    show _ = "I am a Something instance"
addExclamation :: Show a => a -> String
addExclamation v = show v ++ "!!!"The function signature of show is:
show :: Show a => a -> String
- the double arrow =>is applying a constraint ona
- the single arrow is ->is representing the next parameter or return type
In reality, the two arrows are almost the same thing.
Originally, haskell did not have typeclasses. Instead there was a dictionary that worked like a Virtual Method Table.
So if you wanted to create an instance of the Show typeclass, you would create a new type (something like ShowWhatever) and add a pointer to that function into your type function dictionary.
Now, when you create an instance of a typeclass, it still creates VMT in the background for the instance.
#proglang-f19
