简体   繁体   English

javascript-原型内部未定义

[英]javascript - undefined inside prototype

I have this constructor: 我有这个构造函数:

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.lyrics);
};

and then I've created 24 instances, up to: 然后我创建了24个实例,最多:

var song24 = new Song('Lab', 'Buffalo', 23, '3:10', 'Band', 
    ["this", "tambourine", "is", "waging", "a", "war", "will",
    "drenched", "in", "blood", "flood", "egg", "shape",
    "shaped", "rock", "rocking", "to", "kill", "the", "bull",
    "slay", "slain", "by", "dogs", "snakes", "raven", "scorpio",
    "lion", "headed", "head", "god", "rise"]);

I want to intersect user inputs, like... 我想与用户输入相交,例如...

 var input = ["tambourine", "this"];

...with the lyrics database; ...带有歌词数据库; for that purpose I have an intersection function... 为此,我有一个交集功能...

function setIntersection(a, b) {

    var result = [];

    for (var i = 0; i < a.length; i++) {
        if (b.indexOf(a[i]) !== -1 && result.indexOf(a[i]) === -1) {
            result.push(a[i]);
        }
    }

    return result;
}

...and a prototype function : ...和一个prototype function

Song.prototype.songIntersect = function(input) {

    var bestSong = null;
    var bestCount = -Infinity;

    for (var i = 1; i <= 24; i++) {

        var currentSong = ['song' + i];
        var currentCount = setIntersection(song[i].lyrics, input).length;

        if (currentCount > bestCount) {
            bestSong = currentSong;
            bestCount = currentCount;
        }
    }

    return bestSong.name;
}       

but I get "song not defined". 但我得到“歌曲未定义”。 the error must be in the prototype function . 错误必须在prototype function what am I missing here? 我在这里想念什么? thank you. 谢谢。

You're creating 24 songs with variable names song1 , song2 , etc. However, later when you're trying to access them, you're looking for a song using a non-existent array named song . 您正在创建24首歌曲与变量名song1song2 ,等等。但是,后来当你试图访问它们,你正在寻找使用一个不存在的数组命名一首song You could fix this by defining an array of songs first. 您可以通过先定义一组歌曲来解决此问题。 This also means you don't need to know exactly how many songs there are in the code. 这也意味着您不需要确切知道代码中有多少首歌曲。 You could dynamically add or remove songs based on an external source such as a database, public API, or user input. 您可以根据外部来源(例如数据库,公共API或用户输入)动态添加或删除歌曲。

var songs = [];

// add your songs like this
songs.push(new Song(/* ... */));

Also, your currentSong variable is an array containing a string of the name of the song variable. 同样,您的currentSong变量是一个数组,其中包含歌曲变量名称的字符串。 If you store the songs in an array, you could just store the index or the actual song instead. 如果将歌曲存储在数组中,则可以只存储索引或实际歌曲。

Song.prototype.songIntersect = function(input) {
    var bestSong = null;
    var bestCount = -Infinity;

    for (var i in songs) {
        var currentCount = setIntersection(songs[i].lyrics, input).length;

        if (currentCount > best.count) {
            bestSong = song[i];
            bestCount = currentCount;
        }
    }

    return bestSong && bestSong.name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM