use*_*120 15 javascript constructor object typeerror
我有一个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) {
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;
}
}
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
TypeError: album is not a constructor
我找不到问题.我阅读了很多其他帖子,但我找不到类似的问题.可能是因为不允许在另一个对象中创建对象吗?我怎么能解决这个问题?
Den*_*ret 40
这条线
var album = new album(albumName);
Run Code Online (Sandbox Code Playgroud)
遮蔽外部album功能.所以是的,album不是函数内部的构造函数.更确切地说,就undefined在这一点上.
为了避免这种问题,我建议用大写字母命名你的"类":
function Album(name) {
Run Code Online (Sandbox Code Playgroud)
更常见的是,我建议在有疑问时遵循Google风格指南.
| 归档时间: |
|
| 查看次数: |
74770 次 |
| 最近记录: |