1

Yeah, looks insane, but let me explain better. When in Jquery we use for example $('div') its return an Array Collection or something like this: [div#container, div#header, div#logo], the magic thing is that Methods like push, length, pop, join, concat that are primitive methods from Array will not show you. How can I get this behavior?? I want to return a Collection of Elements just like Jquery does but with my own methods (remove, appendHTML, and others) but not the default Array methods. Any Ideas ??

WSD
  • 3,243
  • 26
  • 38
  • You might take a look at Underscore.js's collection methods for inspiration: http://documentcloud.github.com/underscore/#collections – rjz Mar 18 '12 at 17:10
  • possible duplicate of [Array Like Objects in Javascript](http://stackoverflow.com/questions/6599071/array-like-objects-in-javascript) – Rob W Mar 18 '12 at 17:10
  • In short, define numeric properties, a `length` attribute, and assign custom methods to `YourArrayLikeObject.prototype`. http://stackoverflow.com/search?q=jquery+like+array – Rob W Mar 18 '12 at 17:11

3 Answers3

0

try creating an object array like this :

var myObjectArray = [];

function addObject(id){
    if(document.getElementById(id)!=null){
        myObjectArray.push(document.getElementById(id));
        return true;
    }else{
        return false;
    }
}

function removeObject(id){
    for(i in myObjectArray){
       if(myObjectArray[i].getAttribute("id")==id){
           delete myObjectArray[i];
           return true;
        }
    }    
    return false;
}

function getObject(id){
    for(i in myObjectArray){
        if(myObjectArray[i].getAttribute("id")==id) return myObjectArray[i];
    }
    return null;
}
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
  • The Idea is never show the defaults Array Functions (concat, pop, push...) in my likeArray.some(), however when i write likeArray it must show something like ['some','element','another'] – WSD Mar 22 '12 at 03:27
  • Reading in related post i have an Idea... what about this: function likeArray(){ var FINAL = { length:0, splice:function(){ }, add:function (el){ return FINAL[FINAL.length++]=el; }, remove:function(pos){ //my cool stuff to remove } }; return FINAL; } – WSD Mar 22 '12 at 03:30
0

You could create a custom object with a private array where you expose the method you want

var yourCollection = function() {
    var privateArray = [1, 2, 3, 4];

    return {
        myCustomPush: function(el) {
            //use your custom push function or
            privateArray.push(el);

        },
        size: function() {
            return privateArray.length;
        }
    }
}();

yourCollection.myCustomPush("s");
alert(yourCollection.size());
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • Thanks... but when i write (for example in Firebug console) yourCollection, its show an Object like this: Object { myCustomPush=function(), size=function()}, the idea is show an Array Like Object, like this: ['some','foo','bar'], but when i write yourCollection.some() no methods from native Arrays were showed. – WSD Mar 22 '12 at 03:25
0
function() arrayLike {
    var FINAL = {
        length:0,
        splice:function(){}
        add:function(el){
            return FINAL[FINAL.length++]=el;
        };
    }
}

Now we can declare a var like this:

var myarray = arrayLike();
myarray.add('some');
myarray.add('other');

And the magic is that we write in Firebug (for example):

myarray.

Only the add() method will show you, but not the defaults Arrays Method (concat, push, pop...) Any Ideas to improve this code, please post It!! or write me (please!)

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
WSD
  • 3,243
  • 26
  • 38