https://github.com/kormanowsky/collection
JavaScript collection of objects
https://github.com/kormanowsky/collection
Last synced: 12 months ago
JSON representation
JavaScript collection of objects
- Host: GitHub
- URL: https://github.com/kormanowsky/collection
- Owner: kormanowsky
- Created: 2018-06-07T23:07:53.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-07T23:21:54.000Z (about 8 years ago)
- Last Synced: 2025-01-28T02:23:41.019Z (over 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Collection
JavaScript collection of objects.
A Collection is an array of objects of specified class. All elements of a collection must be instances of the same class. You can add, remove, get elements, or do something for each of them like if you use a usual array. This function lets you forget about checking if a variable is an object of class because it does that stuff by itself.
## Creating new Collection
Just create new Collection object:
```javascript
function MyClass(x,y){
this.x = x;
this.y = y;
}
var myCollection = new Collection(MyClass);
```
## Adding new element
Call Collection.add() and you will get the id of new element:
```javascript
var myElementId = myCollection.add(new MyClass(1,2));
```
Then you can use this id to do something with this element.
## Getting the element
Call Collection.get():
```javascript
var myElement = myCollection.get(myElementId);
```
## Removing the element
Call Collection.remove():
```javascript
myCollection.remove(myElementId);
```
## Looping through the elements
```javascript
myCollection.forEach(function(element, index, array){
//something
});
```
## Extending your Collection
```javascript
function myVeryNiceFunction(list){
return list[0];
}
myCollection.extend('veryNiceFunction', myVeryNiceFunction);
var firstElement = myCollection.veryNiceFunction();
```