所以我有一个伪类,里面有函数和变量。例如:
function MyClass()
{
this.a = 0;
this.b = function(c)
{
this.a += c;
}
}
然后,当我以后去使用它时,我会这样做:
var myObject = new MyClass();
myObject.b(3);
myObject.b(5);
但是当我这样做时:
console.log("A: " + myObject.a);
我得到:
A: 0
我究竟做错了什么?
这是我的实际代码。它被分成多个文件,但我会提出相关的文件:
function SongDatabase() {
this.songs = new Array();
this.loadSongs = function () {
};
this.saveSongs = function () {
};
var _Constructor_ = function () {
var mySong = new Song();
this.songs = new Array(mySong);
};
_Constructor_();
}
function LyriX() {
var songDatabase = new SongDatabase();
//var playlistDatabase = new PlaylistDatabase();
songDatabase.loadSongs();
var sourceList = new ScrollableList();
sourceList.setElement($S.getElement("sourceList"));
var accessoryList = new ScrollableList();
accessoryList.setElement($S.getElement("accessoryList"));
var sourceListClick = function (index) {
$S.log("source click: " + index);
if (index == 0) {
displaySongs();
}
};
sourceList.setClickListener(sourceListClick);
var displaySongs = function () {
$S.log("display songs");
// STACK OVERFLOW LOOK HERE!!! thanks :)
// in debug in chrome songDatabase.songs is a zero length array
accessoryList.loadArray(songDatabase.songs);
};
}
$S.addOnLoadListener(function () {
new LyriX();
});