Building objects Function constructors, 'new' and the History of Javascript

function Person(){
    this.firstname:'John';
    this.lastname:'Doe';
}

var john = new Person(); //new keyword is an operator - an empty object is created and then it invokes the function
console.log(john); //Have an object that is a person 

Function Constructors:
A normal function that is used to construct objects.
The 'this' variable points a new empty object and that object is returned from the function automatically

*******************************************************************************************************************************************************************************************************************************
Fuction Constructors and '.prototye'

Anytime we create a function object, it has special properties, 'prototype' never used until "the new operator is invoke"
Lives only when you use a function constructor 

function Person(firstname, lastname){
    this.firstname:'John';
    this.lastname:'Doe';
}

var john = new Person('John', 'Doe');
console.log(john);

//Set the prototype when the 'new operator is called'
Person.prototype.getFullName = function {
    return this.firstname  + this.lastname;
}
Object.create and Pure Prototypcal Inheritance

var person = {
    firstname: 'Default',
    lastname: 'Default',
    greet: function (){
        return 'Hi' + this.firstname; //Using this as we are referencing the object
    }
}

var john = Object.create(person); //.create an empty object and its prototype to the object you passed in 
john.firstname = 'John'; //The pattern is you override the properties of the default values
john.lastname = 'Doe';
console.log(john)

On Chrome dev console:
> john.greet()
"Hi John"

results matching ""

    No results matching ""