-
-
Save kccrs/fc5c7123dd4651a85b1f537a9297f63e to your computer and use it in GitHub Desktop.
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 literal | |
// "" string literal | |
// 23 number literal | |
// [] array literal | |
function makeBurger(pattyType) { | |
return { | |
pattyType: pattyType, | |
toppings: [], | |
addTopping: function (topping) { | |
this.toppings.push(topping); | |
console.log(this, this.toppings); | |
} | |
}; | |
} | |
console.log(cheeseBurger.toppings); | |
describe('makeBurger', function() { | |
it('should make a burger with the pattyType that was passed in', function() { | |
var burger = makeBurger('raccoon'); | |
assert.equal(burger.pattyType, 'raccoon'); | |
}); | |
//We should put a test that makes sure it starts out with a toppings property that has an empty array as value. | |
it('should add a topping with addToppings()', function() { | |
var burger = makeBurger('raccoon'); | |
burger.addTopping('anchovies'); | |
assert.include(burger.toppings, 'anchovies'); | |
}); | |
}); | |
var platonicBurger = { | |
pattyType: 'beef', | |
toppings: [], | |
addTopping: function(topping) { | |
this.toppings.push(topping); | |
console.log(this, this.toppings); | |
} | |
} | |
Object.getPrototypeOf(dumpsterDiver) | |
function makeBurger(pattyType) { | |
Object.create(platonicBurger); | |
} | |
function makeBurger(pattyType) { | |
var newBurger = Object.create(platonicBurger); | |
newBurger.pattyType = pattyType; | |
newBurger.toppings = []; | |
} | |
// When we prefix a function with the "new" keyword, we are using it as a "constructor" of objects. | |
//Functions created a constructors behave a little differently, we name it with a capital letter to help us identify it. | |
function Burger(pattyType) { | |
// all these things happen when you use the "new" keyword **** VERY IMPORTANT - DO NOT FORGET TO USE "new" *** | |
// Create a new object, set it to 'this' | |
// e.g. this = {} | |
// Set the prototype of 'this' to Burger.prototype | |
this.pattyType = pattyType; | |
this.toppings = []; | |
// return this; | |
} | |
console.log(Burger.length) | |
1 // because it only takes one argument | |
Object.getOwnPropertyNames(Burger) | |
//(returns) | |
["length", "name", "arguments", "caller", "prototype"] | |
Burger.prototype | |
// returns | |
Object{} | |
Burger.prototype = {} // it is the object that lives in the burger's prototype property is the object that ends up being the | |
// prototype for everything the burger function creates. | |
// All functions have a property that just happens to be called "prototype". | |
// By default, this is an empty object. | |
// When we use a function wit the "new" keyword, it makes a new object. | |
// Those new objects that it creates are wired up with that empty object that lives in the prototype property of the | |
// constructor function as _their_ actual prototype. | |
// When we use the 'new' keyword. The constructor function will look to its prototype property and set it as the | |
// prototype of all of the instances (burgers) it creates. | |
// modifying existing prototype | |
// all shared methods go on the prototype | |
Burger.prototype.addTopping = function (topping) { | |
this.toppings.push(topping); // it is pushed because it is an array | |
} | |
Burger.prototype.overcook = function () { | |
this.pattyType = 'charcoal'; | |
} | |
jeffsBurger.overcook(); | |
// jeffsBurger now has a pattyType of charcoal | |
jeffsBurger.addTopping('unburnt duck') | |
var jeffsBurger = new Burger('duck'); | |
var petesBurger = new Burger('beef'); | |
var maryJanesBurger = new Burger('turkey'); | |
describe('makeBurger', function() { | |
it('should make a burger with the pattyType that was passed in', function() { | |
var burger = new Burger('raccoon'); | |
assert.equal(burger.pattyType, 'raccoon'); | |
}); | |
//We should put a test that makes sure it starts out with a toppings property that has an empty array as value. | |
it('should add a topping with addToppings()', function() { | |
var burger = new Burger('raccoon'); | |
burger.addTopping('anchovies'); | |
assert.include(burger.toppings, 'anchovies'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment