Skip to content

Instantly share code, notes, and snippets.

@garrettmac
Last active September 15, 2017 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garrettmac/1f60247a1fbcc6c6eeaad01fda2ce7e6 to your computer and use it in GitHub Desktop.
Save garrettmac/1f60247a1fbcc6c6eeaad01fda2ce7e6 to your computer and use it in GitHub Desktop.
MEDIUM BLOG POST
/* in ES5 */
//create Person
function Person () {}
// Person Inheritance a name through the prototype keyword
Person.prototype.name = “Garrett Mac”;
//Person is a constructor function because we will use new keyword to invoke it.
​var person= new Person () //create
console.log(person.name()); // Garrett Mac
/* in ES6 */
//create Person
class Person {
constructor(name){
this.name=name
}
}
//Person is a constructor function because we will use new keyword to invoke it.
var person= new Person("Garrett Mac") // {name: "Garrett Mac"}
console.log(person.name); // Garrett Mac
Object.create = function (o) {
​//It creates a temporary constructor F()​
 function F() {
 }
​//And set the prototype of the this constructor to the parametric (passed-in) o object​
​//so that the F() constructor now inherits all the properties and methods of o​
 F.prototype = o;
​//Then it returns a new, empty object (an instance of F())​
​//Note that this instance of F inherits from the passed-in (parametric object) o object. ​
​//Or you can say it copied all of the o object’s properties and methods​
 return new F();
 }
console.clear()
/* Mixed Example in ES5 */
// Classical Inheritance
function Person (name) {
this.name=name
}
var person= new Person ("Garrett")
console.log(person) //{name: "Garrett"}
// Prototypal Inheritance (add last name )
Person.prototype.last = "Mac";
console.log(person) //{last: "Mac",name: "Garrett"}
var person= new Person ("Dugan")
console.log(person) //{last: "Mac",name: "Dugan"} <-- Prototypal Inheritance overwrites "Garrett"
/* in ES5 */
//create Person
function Person (name) {
this.name=name
}
// Person Inheritance a name through the prototype keyword
Person.prototype.name = "Garrett Mac";
//Person is a constructor function because we will use new keyword to invoke it.
var person= new Person ("Garrett Mac") //{name: "Garrett Mac"}
console.log(person.name); // Garrett Mac
/* in ES5 */
//create Person
function Person () {}
// Person Inheritance a name through the prototype keyword
Person.prototype.name = "Garrett Mac";
//Person is a constructor function because we will use new keyword to invoke it.
var person= new Person () //{name: "Garrett Mac"}
console.log(person.name); // Garrett Mac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment