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