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
let obj = {}; | |
console.log(Object.prototype); // [Object: null prototype] {} | |
console.log(Object.getPrototypeOf(obj)); // [Object: null prototype] {} | |
console.log(Object.prototype === Object.getPrototypeOf(obj)); // true | |
console.log(Object.prototype.isPrototypeOf(obj)); // true | |
Object.prototype.foo = 'Hey there!'; | |
console.log(Object.prototype); // [Object: null prototype] { foo: 'Hey there!' } |
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
let foo = { | |
bar: 1, | |
}; | |
console.log(foo.bar); // 1 | |
console.log(foo.hasOwnProperty('bar')); // true |
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
function Factory() { | |
this.foo = 1; | |
this.bar = 'string'; | |
} | |
Factory.prototype.example = "I'm a property of Factory.prototype"; | |
console.log(Factory.prototype); // { example: "I'm a property of Factory.prototype" } | |
let descendant = new Factory(); | |
console.log(descendant); // Factory { foo: 1, bar: 'string' } |
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
function Factory() { | |
this.foo = 1; | |
this.bar = 'string'; | |
} | |
let descendant = new Factory(); | |
console.log(descendant); // Factory { foo: 1, bar: 'string' } | |
console.log(Factory.prototype); // {} |
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
// This is a continuation of the previous code block | |
Object.prototype.test = `I'm a property of Object.prototype`; | |
console.log(Object.prototype); // [Object: null prototype] | |
// { test: "I'm a property of Object.prototype" } */ | |
console.log(Factory.prototype.test); // I'm a property of Object.prototype | |
console.log(Factory.prototype); // {} | |
console.log(descendant.test); // I'm a property of Object.prototype |
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
function Factory() { | |
this.foo = 1; | |
this.bar = 'string'; | |
} | |
let descendant = new Factory(); | |
console.log(Factory.prototype); // {} | |
console.log(Object.getPrototypeOf(Factory.prototype)); // [Object: null prototype] {} |
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
function objectFactory() { | |
return { | |
property1: 'My first property', | |
property2: 'My second property', | |
printProperties() { | |
for (let prop in this) { | |
if (typeof this[prop] !== 'function') { | |
console.log(`${prop}: ${this[prop]}`); | |
} |
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
function Grandfather(name, age, job) { | |
this.name = name; | |
this.age = age; | |
this.job = job; | |
} | |
Grandfather.prototype.aboutMe = function() { | |
console.log(`I'm ${this.name}, I'm ${this.age} years old and I'm ${this.job === 'retired' ? '' : 'a '}${this.job}.`); | |
}; |
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
class Grandfather { | |
constructor(name, age, job) { | |
this.name = name; | |
this.age = age; | |
this.job = job; | |
} | |
aboutMe() { | |
console.log(`I'm ${this.name}, I'm ${this.age} years old and I'm ${this.job === 'retired' ? '' : 'a '}${this.job}.`); | |
} |
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
let name = 'Eamon'; | |
function printName() { | |
console.log(name); | |
} | |
printName(); // => 'Eamon' | |
name = 'Jack'; | |
printName(); // => 'Jack' |
OlderNewer