Create a List object - Node.js Object

Node.js examples for Object:Object Operation

Description

Create a List object

Demo Code


function List(){  
  this.items = new Array();/*w  w w.  j  ava  2s .c o m*/
}


List.prototype.indexOf = function(obj){
  for (var i = 0; i < this.items.length; i++) {
    if (this.items[i] == obj) {
      return i;
    }
  }
  return -1;
};

List.prototype.get = function(index){
  return this.items[index];
};


List.prototype.add = function(){
  for(var i=0 ; i<arguments.length ; i++){
    if(typeof arguments[i] == 'string'){
      this.items = this.items.concat(arguments[i].split(","));
    }
    else{
      this.items.push(arguments[i]) ;
    }
  }
};


List.prototype.insert = function(index, obj){
  this.items.splice(index, 0 , obj );
};


List.prototype.remove = function(index){
  this.items.splice(index , 1 );
};


List.prototype.removeItem=function(obj){
  var i = this.indexOf(obj);
  if(i != -1){
    this.remove(i);
  }
};


List.prototype.sort = function(){
  this.items.sort();
};


List.prototype.size = function(){
  return this.items.length ;
};


List.prototype.join = function(str){
  return this.items.join(str);
};


List.prototype.toString = function(){
  return this.items.toString();
};

Related Tutorials