Skip to content

Instantly share code, notes, and snippets.

@asternaut
Created March 28, 2017 20:10
Show Gist options
  • Save asternaut/de9ffc435a28e0700dfa1a878a95db6a to your computer and use it in GitHub Desktop.
Save asternaut/de9ffc435a28e0700dfa1a878a95db6a to your computer and use it in GitHub Desktop.
Practicing Methods
function zodiacInfo(sign, dates, element) {
this.sign = sign;
this.dates = dates;
this.element = element;
};
//object literals:
var aquarius = {
sign: "aquarius" ,
dates: "1/20 - 2/18" ,
element: "air"
};
var pisces = {
sign: "pisces" ,
dates: "2/19 -3/20" ,
element: "water"
};
var aries = {
sign: "aries" ,
dates: "3/21 - 4/19" ,
element: "fire"
};
var taurus = {
sign: "taurus" ,
dates: "4/20-5/19" ,
element: "earth"
};
var gemini = {
sign: "gemini" ,
dates: "5/21 - 6/20" ,
element: "water",
// a method
astrologyMethod: function() {
return "If your birthday is between "+ this.dates + ", then your zodiac sign is " + this.sign + ", and your element is " + this.element + ".";
}
};
gemini.element="air"; // change an object literal's value
function astrologyMethod () {
return "If your birthday is between "+ this.dates + ", then your zodiac sign is " + this.sign + ", and your element is " + this.element + ".";
};
astrologyMethod ();
console.log(gemini.astrologyMethod());
console.log(astrologyMethod());
@asternaut
Copy link
Author

Do I need to bind?

@asternaut
Copy link
Author

Ok here's an overview of what I want to accomplish:
I've made 5 object literals, and one has a method which concatenates three values (sign, date, and element) into a simple sentence. I want to

  • write a function that will replace the method -> i.e. I can have one function that can console log the concatenation rather than use a method within every object literal.
  • take it a step further and loop the function, so it can console log every concatenation for every variable.

What I don't know is if I need to push the variables into an array and if I need to bind this to the values..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment