Last active
September 15, 2017 02:52
-
-
Save garrettmac/1f60247a1fbcc6c6eeaad01fda2ce7e6 to your computer and use it in GitHub Desktop.
MEDIUM BLOG POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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