Use var for some non-top-level functions
[conkeror.git] / modules / compat / Map.js
blob26ae68e809c6c3f71f842d7f294dcf96a087c390
1 /**
2  * (C) Copyright 2014 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6  **/
8 /**
9  * Simple emulation of Map for Gecko versions < 13 Keys are based on
10  * string representation of Object, rather than Object identity, but
11  * for numbers and strings the effect is the same.
12  **/
14 if (typeof(Map) == "undefined") {
15     var Map = function Map() {
16         this.data = {}
17         this.size = 0;
18     }
19     Map.prototype.get = function (k) { return this.data["K" + k]; };
20     Map.prototype.set = function (k, v) {
21         if (!(("K" + k) in this.data)) ++this.size;
22         this.data["K" + k] = v;
23     };
24     Map.prototype.has = function (k) { return ("K" + k) in this.data; };
25     Map.prototype.delete = function (k) {
26         if (("K" + k) in this.data) {
27             delete this.data["K" + k];
28             --this.size;
29         }
30     };
31     Map.prototype.clear = function (k) { this.data = {}; this.size = 0; };
33     // Note: This does not return in insertion order.
34     Map.prototype.keys = function () {
35         for (k in this.data)
36             yield k.substring(1);
37     };
39     // Note: This does not return in insertion order.
40     Map.prototype.values = function () {
41         for (k in this.data)
42             yield this.data[k];
43     };
45     // Note: This does not return in insertion order.
46     Map.prototype.entries = function () {
47         for (k in this.data)
48             yield [k.substring(1), this.data[k]];
49     };
51     Map.prototype.forEach = function (callbackFn, thisArg) {
52         for (k in this.data)
53             callbackFn.call(thisArg, k.substring(1), this.data[k]);
54     };