Skip to content

Instantly share code, notes, and snippets.

@jussi-kalliokoski
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jussi-kalliokoski/5ef02ef90c6cbb8c1a70 to your computer and use it in GitHub Desktop.
Save jussi-kalliokoski/5ef02ef90c6cbb8c1a70 to your computer and use it in GitHub Desktop.
Map example with @create and @initialize
import Symbol from "@symbol";
let list = Symbol.new();
export class Map {
constructor () {
// nothing to do here actually
},
// this would be implicit unless specified
static [@@create] (list) {
var instance = Object.create(Map.prototype);
Map[@@initialize].apply(instance, arguments);
return instance;
}
static [@@initialize] (list) {
if ( list == null ) {
list = [];
}
// argument validity checks here.
this[list] = list || [];
}
get (key) {
if ( !this[list] ) {
throw new TypeError("Map.prototype.get must be called on a Map instance");
}
for ( let i = 0; i < this[list].length; i++ ) {
if ( this[list][0] === key ) {
return this[list][1];
}
}
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment