Created
October 7, 2017 18:44
-
-
Save camerican/1c692bfe73e117471cf45b386820a8bc to your computer and use it in GitHub Desktop.
Valerie OO Code Review
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 the beginning of the code for #1 | |
function Multiplier() { | |
// declaring a method within the constructor generally isn't as desirable as | |
// declaring it on the prototype of the constructor (that way all instances use the prototype | |
// method rather than getting their own copy of the method) | |
this.multiply = function(){ | |
// in here you'd want to change the value of a currentValue rather than simply log out the number 0 | |
console.log(' ' * 1); // ' ' space character is converted to 0 and multiplied by 1, which is 0 | |
} | |
var getCurrentValue = new Multiplier; // you're declaring a local variable getCurrentValue and setting it equal | |
// to an instance of the Multiplier -- this is going to call Multiplier again | |
// which calls itself again, and again... resulting in infinite recursion | |
// getCurrentValue should just return the currentValue that has been calculated | |
} | |
return this.Multiplier; // this return statement is outside the context of a function, which results in an error | |
// This is the end of the code for #1 | |
// This is the beginning of the code for #2 | |
function Album() { | |
this.photos = [ | |
] | |
this.photos = 0; // you're overriding the empty array declared above... | |
} | |
Album1 = new Album; // we don't want to use a capital letter for a variable name, only Constants and Constructors | |
Album.prototype.addPhoto = function( src, location ) { | |
let photo = new Photo( src, location ); | |
this.photos.push( photo ); | |
return this; | |
}; | |
Album.prototype.listPhotos = function() { | |
return this.photos.join(", "); | |
}; | |
Album.prototype.getPhoto = function(index) { | |
return this.photos[index]; | |
}; | |
function Photo( src, location ) { | |
this.src = src; | |
this.location = location; | |
} | |
Photo.prototype.toString = function(){ | |
return `${this.src} (${this.location})`; | |
} | |
const myAlbum = new Album(); | |
myAlbum.addPhoto("dog.jpg","Home").addPhoto("Cat","Hat"); | |
// This is the end of the code for #3. | |
var valerie = { | |
name: "Valerie Kirby", | |
teacher: "Cam Crews", | |
school: "NYCDA", | |
greet: function() { | |
console.log("Hi. My name is " + valerie.name + "." +"My teacher is " + valerie.teacher + "and my school is " valerie.school); | |
} | |
} | |
//Not sure if this works completely so I'm adding additional code for #3 below: | |
function Person( firstName, lastName ) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
Person.prototype.fullName = function() { | |
return `${this.firstName} ${this.lastName}`; | |
} | |
function Teacher( firstName, lastName, email ) { | |
this.email = email; | |
Person.call(this, firstName, lastName); | |
} | |
Teacher.prototype = Object.create(Person.prototype); | |
function Student( Valerie, Kirby, Cams ) { // the parameters here should probably be firstName, lastName, and email still | |
this.cohort = arguments[arguments.length-1]; | |
Person.apply(this, Array.from(arguments).slice(0,2)); | |
} | |
Student.prototype = Object.create(Person.prototype); | |
function School( ) { | |
this.people = []; | |
} | |
School.prototype.addPerson = function( person ) { | |
this.people.push( person ); | |
return this; | |
const nycda = new School(); | |
nycda.addPerson( new Teacher("Cam","Crews","cam@nycda.com")).addPerson( new Student("Jack","Floyd","WDI 2017")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment