Building objects Function constructors, 'new' and the History of Javascript
function Person(){
this.firstname:'John';
this.lastname:'Doe';
}
var john = new Person();
console.log(john);
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);
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;
}
}
var john = Object.create(person);
john.firstname = 'John';
john.lastname = 'Doe';
console.log(john)
On Chrome dev console:
> john.greet()
"Hi John"