admin管理员组文章数量:1392116
I'm trying to implement a collection in Javascript - is there anyway to implement an array-like indexer for elements in my collection?
So far, I have the following code:
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;
};
};
All of the Array.prototype
methods and properties work as expected.
var c = Collection();
c.push(4);
console.log(c.length); // 1
But the one thing I can't figure out is how to get the following to work:
console.log(c[0]); // should print 4, currently undefined
Is there anyway to do this?
I'm trying to implement a collection in Javascript - is there anyway to implement an array-like indexer for elements in my collection?
So far, I have the following code:
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;
};
};
All of the Array.prototype
methods and properties work as expected.
var c = Collection();
c.push(4);
console.log(c.length); // 1
But the one thing I can't figure out is how to get the following to work:
console.log(c[0]); // should print 4, currently undefined
Is there anyway to do this?
Share Improve this question asked Sep 25, 2012 at 15:34 BillBill 25.6k8 gold badges99 silver badges129 bronze badges 2-
1
You can't. Any specific reason you're not just using an
Array
? – jbabey Commented Sep 25, 2012 at 15:36 -
I think something is missing in your code.
collection
is never populated, so no Array prototypes are copied. Can you post a working fiddle? – David Hellsing Commented Sep 25, 2012 at 15:42
2 Answers
Reset to default 4If you want to "extend" Array, the classic way would be something like:
function Collection(){};
Collection.prototype = new Array();
Collection.constructor = Collection;
Now add your own methods:
Collection.prototype.color = function() {
this.push('color');
};
And use it with new
:
var myArray = new Collection();
myArray.push(1);
myArray.color();
If you want to add a new push
method that access Array push, try:
Collection.prototype.push = function() {
console.log('pushed!');
Array.prototype.push.apply(this, [].slice.call(arguments));
};
using classes:
class Collection extends Array {
color() {
this.push('color')
}
}
本文标签: How to implement an arraylike indexer ( ) in JavascriptStack Overflow
版权声明:本文标题:How to implement an array-like indexer ([ ]) in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744761301a2623769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论