Last active
July 30, 2018 23:00
-
-
Save kristyburge/c79ac96dc1d96f01e7ec5edd866faf33 to your computer and use it in GitHub Desktop.
More examples using new and the 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
// Using the Dog prototype, create a new instance | |
var chester = new Dog('beagle', 'Chester', ['Gracie', 'Josey', 'Barkley']); | |
chester.intro(); // returns Hi, my name is Chester and I'm a beagle | |
console.log(chester); // returns Dog {breed: "beagle", name: "Chester", friends: Array(3), intro: ƒ} | |
// Here's another example: | |
var City = function(city, state) { | |
this.city = city || "Phoenix"; | |
this.state = state || "AZ"; | |
this.sentence = function() { | |
console.log(`I live in ${this.city}, ${this.state}.`); | |
}; | |
}; | |
var phoenix = new City(); // use the default parameters | |
console.log(phoenix); // returns the phoenix object (an instance of the City prototype) | |
phoenix.sentence(); // returns "I live in Phoenix, AZ." | |
var spokane = new City('Spokane', 'WA'); | |
console.log(spokane); // returns the spokane object (another instance of the City prototype) | |
spokane.sentence(); // returns "I live in Spokane, WA." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment