-
-
Save fresh5447/fd57b6c961a4d60f0838e19a49038ac5 to your computer and use it in GitHub Desktop.
Practicing Methods
This file contains hidden or 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
// ----------- | |
// W/ CONSTRUCTOR | |
// --------- | |
// The constructor pattern is reccomened for this use case, because | |
// you only have to define the function once, then every Object | |
// will have access to that function. | |
function ZodiacInfo(sign, dates, element) { | |
this.sign = sign; | |
this.dates = dates; | |
this.element = element; | |
}; | |
// to pass in param, update function (i think you are just missing the param. | |
ZodiacInfo.prototype.astrologyMethod = function (funnyWord) { | |
return "If your birthday is between "+ this.dates + | |
", then your zodiac sign is " + this.sign + | |
", and your element is " + this.element + "and a funny word is " + funnyWord; | |
}; | |
var aquarius = new ZodiacInfo("aquarius", "1/20 - 2/18", "air"); | |
aquarius.astrologyMethod("GLORP"); | |
// ----------- | |
// Without CONSTRUCTOR | |
// --------- | |
var gemini = { | |
sign: "gemini" , | |
dates: "5/21 - 6/20" , | |
element: "water", | |
// a method | |
astrologyMethod: function(funnyWord) { | |
return "If your birthday is between "+ this.dates + | |
", then your zodiac sign is " + this.sign + | |
", and your element is " + this.element + "and a funny word " + funnyWord; | |
} | |
}; | |
console.log(gemini.astrologyMethod("GLORP)); | |
// ----------- | |
// Without CONSTRUCTOR adding function outside of function | |
// --------- | |
var gemini = { | |
sign: "gemini" , | |
dates: "5/21 - 6/20" , | |
element: "water", | |
}; | |
gemini.astrologyMethod = function(funnyWord) { | |
return "If your birthday is between "+ this.dates + | |
", then your zodiac sign is " + this.sign + | |
", and your element is " + this.element + "and a funny word " + funnyWord; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment