JavaScript扩展数组并向子protype添加方法?
我想创建一个类似于简单音乐播放列表(数组)的js类。我想用ID实例化这个播放列表,每个ID都是我数据库中的一个曲目ID。我有这样的界面:JavaScript扩展数组并向子protype添加方法?,javascript,arrays,Javascript,Arrays,我想创建一个类似于简单音乐播放列表(数组)的js类。我想用ID实例化这个播放列表,每个ID都是我数据库中的一个曲目ID。我有这样的界面: function Playlist() { Playlist.prototype.current = 0; Playlist.prototype.prev = function() { if (this.current-1 < 0) { return null; }
function Playlist() {
Playlist.prototype.current = 0;
Playlist.prototype.prev = function() {
if (this.current-1 < 0) {
return null;
}
return this[--this.current];
};
Playlist.prototype.next = function() {
if (this.current+1 >= this.length) { // length is index + 1
return null;
}
return this[++this.current];
};
Playlist.prototype.seek = function(id) {
for (i in this) {
if (this[i] == id) {
this.current = parseInt(i);
return i;
}
}
return false;
};
Playlist.prototype.getCurrent() {
return this.current;
};
};
var newPlaylist = Playlist(2,3,5,10/* those are my ids */);
目前我发现的唯一方法是:
Playlist.prototype = new Array(2, 3, 5, 10/* those are my ids */);
这没有任何意义,因为它可以被实例化为不同的对象。欢迎任何意见 最佳方式-嵌套数组
function Playlist() {
this.current = 0;
this.list = Array.prototype.slice.call(arguments);;
};
Playlist.prototype.prev = function() {
if (this.current-1 < 0) {
return null;
}
return this.list[--this.current];
};
Playlist.prototype.next = function() {
if (this.current+1 >= this.list.length) { // length is index + 1
return null;
}
return this.list[++this.current];
};
Playlist.prototype.getCurrent = function() {
return this.current;
};
var newPlaylist = new Playlist(2,3,5,10/* those are my ids */);
最佳方式-嵌套数组
function Playlist() {
this.current = 0;
this.list = Array.prototype.slice.call(arguments);;
};
Playlist.prototype.prev = function() {
if (this.current-1 < 0) {
return null;
}
return this.list[--this.current];
};
Playlist.prototype.next = function() {
if (this.current+1 >= this.list.length) { // length is index + 1
return null;
}
return this.list[++this.current];
};
Playlist.prototype.getCurrent = function() {
return this.current;
};
var newPlaylist = new Playlist(2,3,5,10/* those are my ids */);
既然您这样做了,您应该使用播放列表
构造函数构建包装器对象:
Playlist = (function() {
function Playlist(list) {
this.list = list || [];
}
Playlist.prototype.current = 0;
Playlist.prototype.prev = function() {
if (this.current <= 0)
return null;
return this.list[--this.current];
};
Playlist.prototype.next = function() {
if (this.current+1 >= this.length)
return null;
return this.list[++this.current];
};
Playlist.prototype.seek = function(id) {
return this.list.indexOf(id);
};
return Playlist;
})();
既然您这样做了,您应该使用播放列表
构造函数构建包装器对象:
Playlist = (function() {
function Playlist(list) {
this.list = list || [];
}
Playlist.prototype.current = 0;
Playlist.prototype.prev = function() {
if (this.current <= 0)
return null;
return this.list[--this.current];
};
Playlist.prototype.next = function() {
if (this.current+1 >= this.length)
return null;
return this.list[++this.current];
};
Playlist.prototype.seek = function(id) {
return this.list.indexOf(id);
};
return Playlist;
})();
a) 原型方法不在构造函数内b)a)原型方法不在构造函数内b)我唯一不喜欢的是,我不能将这些定义作为单个单元(函数)。也许这是我用JS所能得到的最好的结果。看看用“单位”构建代码的模块模式。我读了这篇文章:不过,我不太确定上面的代码用这个来实现会是什么样子。我知道这太多了,但是您能给我展示一下模块模式的相同示例吗?非常感谢!这里没有太多内容(因为您并不真正需要它),但我已经修改了答案,向您展示了代码示例。我唯一不喜欢的是,我不能将这些定义作为单个单元(函数)。也许这是我用JS所能得到的最好的结果。看看用“单位”构建代码的模块模式。我读了这篇文章:不过,我不太确定上面的代码用这个来实现会是什么样子。我知道这太多了,但是您能给我展示一下模块模式的相同示例吗?非常感谢!这里没有太多内容(因为您并不真正需要它),但我已经修改了答案,向您展示了代码示例。