Skip to content

Instantly share code, notes, and snippets.

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;
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}
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] }
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');
};
const bear = {
claws: true,
diet: 'carnivore'
};
function PolarBear() {
// ...
}
// PolarBear.prototype = bear;
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 }
var myObject = {
// define a getter for `a`
get a() {
return this._a_;
},
// define a setter for `a`
set a(val) {
this._a_ = val * 2;
}
var myObject = {
// define a getter for `a`
get a() {
return 2;
}
};
Object.defineProperty(
myObject, // target
"b", // property name