0

Lets say I have a double album, and I create one object for each side:

 var side1 = {};
 var side2 = {};

For each song I can create an object with its properties:

song1 = {
side: "side1",
name: "Wicked",
track: 1,
duration: 3.45,
author: "Me",
playing: function(){return "playing" + this.name;},
lyrics: ["politicians", "politician", "politics", "telling", 
           "lies", "lie", "to", 
           "media", "the", "youngsters", "young", "elders", 
           "time", 
           "that", "passes", "pass", "by", "oh", "no", "lie", 
           "detector", "detection", "souls", "as", "far", 
           "illusion", 
           "goes", "all", "sinners", "sin", "around", "sun", 
           "earth", "atom", "atoms", "mind", "angels", "angel", 
           "prophet", 
           "prophets", "martyr", "knives", "elder", "detect", 
           "shit", "flies", "fly", "meat", "is", 
           "knife", 
           "and", "death", "life", "I", "am", "gonna", "going", 
           "cast","a", "sacred", "circle"]

     };

If I want to loop through the song1, I can create a function:

   for(var w in song1.lyrics){
      console.log(song1.lyrics[w]);

But how can I use a function like this loop through the arrays of ALL lyrics? Inheritance?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • `function` ? What you mean by `function` here ? I see only `for-in` loop – Vigneswaran Marimuthu Apr 29 '15 at 06:11
  • I don't quite understand what you want to achieve but you should avoid using for/in loop to iterate over arrays, use classic for loop instead (see [why it's a bad idea](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea)) – stambikk Apr 29 '15 at 06:23
  • sorry about the obtuseness. the `function Lyrics( )` is missing. what I want to achieve is to create 10 song `objects`, such as the one in the example, all of them with the `property` `lyrics`. then I want to loop through all 10 lyrics (song1, song2, song3, song4, song5 etc) from the `side1`, `class`, that is, from a method pertaining to `var side1 ={ }` `object`. In other words: is it possible to I create a lyrics method from a superclass and make the child objects (song1, song2, song3) inherit it? hope I made myself clear. @stambikk, @Vigneswaran Marimuthu – 8-Bit Borges Apr 30 '15 at 02:16

1 Answers1

0

Ok, so my guess is that you are not actually interested in inheritance in this case and that you want to create a 'class' song that you would then create instances of, which would subsequently contain the same lyrics method. Here is one way of doing it in javascript:

// Define the Song constructor
var Song = function(name, side, track, duration, author, lyrics) {
  this.name = name;
  this.side = side;
  this.track = track;
  this.duration = duration;
  this.author = author;
  // more properties ...
  this.lyrics = lyrics;
};

// Add a method to Song.prototype
Song.prototype.playLyrics = function(){
  for(var i = 0; i < this.lyrics.length; i++){
    console.log(this.lyrics[i]);
  }
};

// Now you can create Song objects that will all have the playLyrics method
var song = new Song('Wish You Were Here', 'Side 2', 2, '5:15', 'Pink Floyd', ['So', 'so', 'you', 'think', 'you', 'can', 'tell']);

// and you can call the method playLyrics
song.playLyrics();

Please also note that javascript is a prototype-based language and if I misunderstood your question, look at these articles that explain well how it all works: Object-oriented javascript, Inheritance and prototype chain

stambikk
  • 1,175
  • 12
  • 23