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 Organism = { | |
canReproduce: true | |
}; | |
Animal.prototype = Object.create(Organism); | |
Animal.prototype.walk = function () { | |
console.log('Animals have the ability to walk'); | |
}; | |
Animal.prototype.canReproduce = false; |
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 Organism = { | |
canReproduce: true | |
}; | |
Animal.prototype = Object.create(Organism); | |
Animal.prototype.walk = function () { | |
console.log('Animals have the ability to walk'); | |
}; | |
/** ------------ Animal.prototype *does not* mutate its parent, Organism ------------*/ | |
console.log(Organism); // {canReproduce: 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
let Organism = { | |
canReproduce: true | |
}; | |
Animal.prototype = Organism; | |
Animal.prototype.walk = function () { | |
console.log('Animals have the ability to walk'); | |
}; | |
/** ------------ Animal.prototype mutates its parent, Organism ------------*/ | |
console.log(Organism); // { canReproduce: true, walk: [Function] } |
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 Organism = { | |
canReproduce: true | |
}; | |
Animal.prototype = Organism; | |
Animal.prototype.walk = function () { | |
console.log('Animals have the ability to walk'); | |
}; | |
function Animal () { |
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 Organism = { | |
canReproduce: true | |
}; | |
Animal.prototype.walk = function () { | |
console.log('Animals have the ability to walk'); | |
}; | |
function Animal () { | |
this.legs = 4; |
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 Animal () { | |
// 'cat' would have a property 'legs' on itself when 'Animal' is called | |
this.legs = 4; | |
} | |
// 'Animal.prototype' can be thought of as the parent | |
Animal.prototype.walk = function () { | |
console.log('Animals have the ability to walk'); | |
}; |
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
const bear = { | |
claws: true, | |
diet: 'carnivore' | |
}; | |
function PolarBear() { | |
// ... | |
} | |
// PolarBear.prototype = bear; |
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
var myArray = [ 1, 2, 3 ]; | |
var it = myArray[Symbol.iterator](); | |
it.next(); // { value:1, done:false } | |
it.next(); // { value:2, done:false } | |
it.next(); // { value:3, done:false } | |
it.next(); // { done: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
var myObject = { | |
// define a getter for `a` | |
get a() { | |
return this._a_; | |
}, | |
// define a setter for `a` | |
set a(val) { | |
this._a_ = val * 2; | |
} |
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
var myObject = { | |
// define a getter for `a` | |
get a() { | |
return 2; | |
} | |
}; | |
Object.defineProperty( | |
myObject, // target | |
"b", // property name |