Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

array order #55

Open
ghost opened this issue Sep 26, 2013 · 0 comments
Open

array order #55

ghost opened this issue Sep 26, 2013 · 0 comments

Comments

@ghost
Copy link

ghost commented Sep 26, 2013

can be do this way

_=undefined;
function Arr(){
    var order=[];
    var current=0;
    var counter=0;
    this.length=function(return order.length);
    for(var i=0;i<arguments.length;i+=2){
        if (arguments[i]!=undefined){
            this._(arguments[i]);
            this[arguments[i]]=arguments[i+1];
        }else{
            this.push(arguments[i+1]);
        }
    }
    this._=function(name){
        if (!order[name]){
            order.push(name);
        }
        return undefined;
    };
    this.push=function(val){
        order.push(counter);
        this[counter++]=val;
    };
    this.current=function(){
        return this[order[current]];
    };
    this.next=function(){
        return current++;
    };
    this.key=function(){
        return order[current];
    };
    this.hasNext=function(){
        return current<order;
    };
    // implement other array methods: delete,toString ...
}

var arr = new Arr('a',1,_,2,'c',3);// $arr = array('a'=>1,2,'c'=>3);
arr.push(4);  // $arr[]=4; or better arr[arr.counter()]=5
var testLeftSide = (arr._('d')||(arr['d']=5));    //$test = $arr['d']=5;
for(var i,val;arr.hasNext();i=arr.key(),val=arr.current(),arr.next();){} // foreach as $i=>$val
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants