15
我有一个类型错误的问题:的Javascript:类型错误:......是不是构造
function artist(name) {
this.name = name;
this.albums = new Array();
this.addAlbum = function(albumName) {
for (var i = 0; i < this.albums.length; i++) {
if (this.albums[i].name == albumName) {
return this.albums[i];
}
}
var album = new album(albumName);
this.albums.push(album);
return album;
}
}
function album(name) {
this.name = name;
this.songs = new Array();
this.picture = null;
this.addSong = function(songName, track) {
var newSong = new songName(songName, track);
this.songs.push(newSong);
return newSong;
}
}
提供了以下错误:
TypeError: album is not a constructor
我不能发现问题。我读了很多其他帖子,但我找不到类似的问题。难道它不允许在另一个对象中创建对象吗?我如何解决这个问题?
它相当于'var album; album =新专辑(albumName);'。这应该更明显。 – 2013-02-21 17:41:21
@ Felix Kling:也感谢您的帮助!我写了“var album; album = new Album(albumName);” =>但没有成功 – user2089120 2013-02-21 17:50:03
@ user2089120请参阅http://jsbin.com/uhapel/1/edit(打开控制台) – 2013-02-21 17:53:13