我有这个构造函数:
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 :(得分:0)
我认为您正试图通过歌词来搜索歌曲的名称? 的 1。使用歌曲作为参数
function intersection(input, song ){
}
2.您可以按全局变量存储所有歌曲,例如
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
}