我想在Javascript中實現一個集合 - 有無論如何實現我的集合中的元素數組索引器?如何在Javascript中實現類似數組的索引器([])?
到目前爲止,我有以下代碼:
var Collection = function() {
var collection = [];
var addAccessor = function(api, name) {
if (toString.call(collection[name]) == '[object Function]') {
api[name] = (function(){
return function() {
return collection[name](arguments);
};
}());
}
else {
Object.defineProperty(api, name, {
get: function() { return collection.length; },
enumerable: true,
configurable: true
});
}
};
var publicApi = {};
var methods = Object.getOwnPropertyNames(Array.prototype);
for(var i = 0, len = methods.length; i < len; ++i) {
var method = methods[i];
addAccessor(publicApi, method);
}
return publicApi;
};
};
所有的Array.prototype
方法和屬性達到預期效果。
var c = Collection();
c.push(4);
console.log(c.length); // 1
但有一件事我無法弄清楚是怎麼得到以下工作:
console.log(c[0]); // should print 4, currently undefined
反正有沒有做到這一點?
你不能。任何具體的原因你不只是使用'數組'? – jbabey
我認爲你的代碼中缺少一些東西。 'collection'永遠不會被填充,所以沒有Array原型被複制。你可以發佈一個工作小提琴嗎? – David