adkt
4/8/2018 - 11:40 PM

Prototypes Add

This demonstrates how use of the prototype keyword can allow object properties and functions to be added after a js object declaration ("constructor").

function FirstObject(){
    this.test1 = "test1";
}

function SecondObject(){
    FirstObject.call(this);
    this.test2 = "test2";
}

// define prototypes for inheritance
SecondObject.prototype = Object.create(FirstObject.prototype);
SecondObject.prototype.test3 = "test3";

/*** Main ***/
function main() {
    var myTest = new SecondObject();

    console.log(myTest.test1); //test1
    console.log(myTest.test2); //test2
    console.log(myTest.test3); //test3
}

main();