-
-
Save jussi-kalliokoski/5ef02ef90c6cbb8c1a70 to your computer and use it in GitHub Desktop.
Map example with @create and @initialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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