본문 바로가기

Programming/JavaScript

[javascript] map, list 생성

========================

function Map() {

 this.elements = {};

 this.length = 0;

}

 

Map.prototype.put = function(key,value) {

 this.length++;

 this.elements[key] = value;

}

 

Map.prototype.get = function(key) {

 return this.elements[key];

}

 

var map = new Map();

map.put("name","값");

 

console.log(map.get("name"));

console.log(map.length);

 

========================

function List() {

   this.elements = {};

   this.idx = 0;

   this.length = 0;

}

 

List.prototype.add = function(element) {

   this.length++;

   this.elements[this.idx++] = element;

};

 

List.prototype.get = function(idx) {

   return this.elements[idx];

};

 

var list = new List();

 

list.add("값1");

list.add("값2");

 

console.log(list.get(0));

console.log(list.get(1));

console.log(list.length);