s1eepercat
10/3/2019 - 12:51 PM

Advanced objects, this, instantiation, cloning, deep cloning


//Context (using THIS refers to an object it is inside)

const object = {
    a: function () {
        console.log(this);
    }
}

object.a();



//Instantiation 
class Player {

    constructor(name, type) {
        this.name = name;
        this.type = type;
    }

    introduce() {
        console.log(`hello my ${this.name}, I am ${this.type} bro!`)
    }
}

class Wizard extends Player {

    constructor(name, type) {
        super(name, type)
    }

    speak() {
        console.log(`hows it going, ${this.name}?`)
    }
}

const bro = new Wizard('Titas', 'Assmagic');
bro.speak();
bro.introduce();


//Cloning an object 

const a = { a: "a" };

const b = { ...a };
const c = Object.assign({}, a);


//Deep cloning with all other objects inside of the object

const bigO = {
    a: "1",
    b: {
        a: 3,
        v: 15
    }
}

const newBigO = JSON.parse(JSON.stringify(bigO)); //Difficult for a browser with big objects
console.log(newBigO);