Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:TypeError:。。。不是构造函数_Javascript_Object_Constructor_Typeerror - Fatal编程技术网

Javascript:TypeError:。。。不是构造函数

Javascript:TypeError:。。。不是构造函数,javascript,object,constructor,typeerror,Javascript,Object,Constructor,Typeerror,我有一个打字错误问题: 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) {

我有一个打字错误问题:

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;
    }
}
功能艺术家(姓名){
this.name=名称;
this.albums=新数组();
this.addAlbum=函数(albumName){
for(var i=0;i
给出以下错误:

TypeError:相册不是构造函数

我找不到问题。我读了很多其他的帖子,但是我没有发现类似的问题。是否不允许在另一个对象中创建对象?如何解决这个问题?

这行

var album = new album(albumName);
对外部
相册
功能进行阴影处理。因此,是的,
album
不是函数内部的构造函数。更准确地说,在这一点上它是未定义的

为了避免此类问题,我建议您以大写字母开始命名“类”:

function Album(name) {

一般来说,我建议在有疑问时遵循。

这相当于
var-album;唱片集=新唱片集(唱片集名称)。这应该会让事情变得更加明显。@Felix Kling:也谢谢你的帮助!我写了“var-album;album=new-album(album-name);”=>但是没有成功,谢谢你帮我摆脱了困境。很好的解释。