javascript - how to create inheritance -


lets have double album, , create 1 object each side:

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

for each song can create object 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 want loop through song1, can create function:

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

but how can use function loop through arrays of lyrics? inheritance?

ok, guess not interested in inheritance in case , want create 'class' song create instances of, subsequently contain same lyrics method. here 1 way of doing in javascript:

// define 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 method song.prototype song.prototype.playlyrics = function(){   for(var = 0; < this.lyrics.length; i++){     console.log(this.lyrics[i]);   } };  // can create song objects have playlyrics method var song = new song('wish here', 'side 2', 2, '5:15', 'pink floyd', ['so', 'so', 'you', 'think', 'you', 'can', 'tell']);  // , can call method playlyrics song.playlyrics(); 

please note javascript prototype-based language , if misunderstood question, @ these articles explain how works: object-oriented javascript, inheritance , prototype chain


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -