Skip to content

Instantly share code, notes, and snippets.

@fresh5447
Forked from asternaut/challenge33.js
Last active March 28, 2017 23:35
Show Gist options
  • Save fresh5447/fd57b6c961a4d60f0838e19a49038ac5 to your computer and use it in GitHub Desktop.
Save fresh5447/fd57b6c961a4d60f0838e19a49038ac5 to your computer and use it in GitHub Desktop.
Practicing Methods
// -----------
// 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