2015-05-04 60 views
0

我有這樣的構造:的JavaScript - 交叉與不同的原型

var Song = function(side, name, index, duration, author, lyrics) { 
    this.side = side; 
    this.name = name; 
    this.index = index; 
    this.duration = duration; 
    this.author = author; 
    this.lyrics = lyrics; 
    globalLyrics.push(this); 
    }; 

然後我創建的歌曲實例:

var song1 = new Song('Mithras', 'Wicked', 1, '3:45', 'Me and The Plant', 
      ["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"]); 


    var song2 = new Song('Mithras', 'Requiem', 11, '3:25', 'Me and The Plant', 
     ["you", "want", "wanna", "get", "some", "and", "what", 
     "is", "a", "sad", "song", "move", "along", "then", "trip", 
     "on", "are", "try", "trying", "to", "belong", "oh", "no", "it", 
     "the", "go", "up", "like", "king", "kong", "come", "coming", 
     "down", "goes", "minor", "key", "unlock", "unlocks", 
     "moans", "moan", "all", "integrity", "just", "breaks", "bones", 
     "I", "wait", "for", "in", "line", "that", "will", "bring", "me", 
     "home", "wrong", "feels", "feel", "right", "wretchedness", 
     "wretch", "among", "amongst", "so", "warm", "never", "be", 
     "alone", "because", "cause", "have", "these", "life", "short", 
     "break", "politicians"]); 

後來我加入到原型的方法:

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

可以說我有一個用戶輸入(稍後在表單上):

var input = ["politicians"]; 

而且一個路口功能:

function intersection (input, lyrics){ 
     return input.filter(function(n){ 
     return lyrics.indexOf(n) != -1 
     }); 
    } 

問題:

由於intersection()不是宋prototype,我怎麼做一個函數返回歌曲的名稱的一部分 - 相對於this.name歌詞this.lyrics與用戶輸入有最多交集?

總之,鑑於上面的例子中,我要進入var input = ["politician"]return //Wicked

回答

0

我認爲你正試圖使該功能通過歌詞搜索歌曲的名字?
1.使用歌曲作爲參數

function intersection(input, song){ 

} 

2.You可以通過全局變量存儲所有的歌曲像

var songList = []; 

// Push the song on the list when you make the song 
var song1 = new Song(...); 
songsList.push(song1); 

// Using songsList to make your search 

function intersection(input){ 
// Find the name of song by loop the songList 
}