-
-
Save Raynos/1638059 to your computer and use it in GitHub Desktop.
Revisions
-
Raynos revised this gist
Mar 24, 2014 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,8 @@ window.WeakMap = window.WeakMap || (function () { return { get: function (key, fallback) { var store = privates(key) return store.hasOwnProperty("value") ? store.value : fallback }, set: function (key, value) { privates(key).value = value @@ -19,12 +20,13 @@ window.WeakMap = window.WeakMap || (function () { } function namespace(obj, key) { var store = { identity: key }, valueOf = obj.valueOf Object.defineProperty(obj, "valueOf", { value: function (value) { return value !== key ? valueOf.apply(this, arguments) : store }, writable: true }) @@ -36,7 +38,8 @@ window.WeakMap = window.WeakMap || (function () { var key = {} return function (obj) { var store = obj.valueOf(key) return store && store.identity === key ? store : namespace(obj, key) } } }()) -
Raynos revised this gist
Mar 24, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,8 @@ window.WeakMap = window.WeakMap || (function () { return { get: function (key, fallback) { var store = privates(key) return store.hasOwnProperty("value") ? store.value : fallback }, set: function (key, value) { privates(key).value = value -
Raynos revised this gist
Mar 24, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,8 @@ window.WeakMap = window.WeakMap || (function () { return { get: function (key, fallback) { var content = privates(key).value return content === undefined ? fallback : content }, set: function (key, value) { privates(key).value = value -
Raynos revised this gist
Apr 14, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // Original - @Gozola. This is a reimplemented version (with a few bug fixes). window.WeakMap = window.WeakMap || (function () { var privates = Name() return { -
Raynos revised this gist
Apr 14, 2012 . No changes.There are no files selected for viewing
-
Raynos revised this gist
Apr 14, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ window.WeakMap = WeakMap || (function () { valueOf = obj.valueOf Object.defineProperty(obj, "valueOf", { value: function (value) { return value !== key ? valueOf.apply(this, arguments) : store }, writable: true @@ -33,7 +33,7 @@ window.WeakMap = WeakMap || (function () { function Name() { var key = {} return function (obj) { var store = obj.valueOf(key) return store !== obj ? store : namespace(obj, key) } -
Raynos revised this gist
Apr 14, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ window.WeakMap = WeakMap || (function () { "delete": function (key) { return delete privates(key).value } } function namespace(obj, key) { var store = {}, @@ -38,4 +38,4 @@ window.WeakMap = WeakMap || (function () { return store !== obj ? store : namespace(obj, key) } } }()) -
Raynos revised this gist
Apr 14, 2012 . 1 changed file with 15 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,40 +1,40 @@ // Original - @Gozola. This is a reimplemented version (with a few bug fixes). window.WeakMap = WeakMap || (function () { var privates = Name() return { get: function (key, fallback) { return privates(key).value || fallback }, set: function (key, value) { privates(key).value = value }, has: function(key) { return "value" in privates(key) }, "delete": function (key) { return delete privates(key).value } }; function namespace(obj, key) { var store = {}, valueOf = obj.valueOf Object.defineProperty(obj, "valueOf", { value: function _valueOf(value) { return value !== key ? valueOf.apply(this, arguments) : store }, writable: true }) return store } function Name() { var key = {} return function name(obj) { var store = obj.valueOf(key) return store !== obj ? store : namespace(obj, key) } } -
Raynos revised this gist
Jan 19, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // Original - @Gozola. This is a reimplemented version (with a few bug fixes). window.WeakMap = WeakMap || (function () { var namespace = Name(); -
Raynos revised this gist
Jan 19, 2012 . 1 changed file with 36 additions and 96 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,100 +1,40 @@ window.WeakMap = WeakMap || (function () { var namespace = Name(); return { get: function get(key, fallback) { return namespace(key).value || fallback; }, set: function set(key, value) { namespace(key).value = value; }, has: function has(key) { return "value" in namespace(key); }, delete: function delete(key) { return delete namespace(key).value; } }; function namespace(obj, key) { var store = {}, valueOf = obj.valueOf; Object.defineProperty(obj, "valueOf", { value: function _valueOf(value) { return value !== key ? valueOf.apply(this, arguments) : store; }, writable: true }); return store; } function Name() { var key = {}; return function name(obj) { var store = obj.valueOf(key); return store !== obj ? store : namespace(obj, key) } } }()); -
Gozala created this gist
Oct 7, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,100 @@ /* vim:set ts=2 sw=2 sts=2 expandtab */ /*jshint asi: true undef: true es5: true node: true devel: true forin: false latedef: false */ /*global define: true */ if (typeof(WeakMap) === 'undefined') WeakMap = (function(global) { "use strict"; function defineNamespace(object, namespace) { /** Utility function takes `object` and `namespace` and overrides `valueOf` method of `object`, so that when called with a `namespace` argument, `private` object associated with this `namespace` is returned. If argument is different, `valueOf` falls back to original `valueOf` property. **/ // Private inherits from `object`, so that `this.foo` will refer to the // `object.foo`. Also, original `valueOf` is saved in order to be able to // delegate to it when necessary. var privates = Object.create(object), base = object.valueOf Object.defineProperty(object, 'valueOf', { value: function valueOf(value) { // If `this` or `namespace` is not associated with a `privates` being // stored we fallback to original `valueOf`, otherwise we return privates. return value != namespace || this != object ? base.apply(this, arguments) : privates }}) return privates } function Name() { /** Desugared implementation of private names proposal. API is different as it's not possible to implement API proposed for harmony with in ES5. In terms of provided functionality it supposed to be same. http://wiki.ecmascript.org/doku.php?id=strawman:private_names **/ var namespace = {} return function name(object) { var privates = object.valueOf(namespace) return privates !== object ? privates : defineNamespace(object, namespace) } } function guard(key) { /** Utility function to guard WeakMap methods from keys that are not a non-null objects. **/ if (key !== Object(key)) throw TypeError("value is not a non-null object") return key } function WeakMap() { /** Implementation of harmony `WeakMaps`, in ES5. This implementation will work only with keys that have configurable `valueOf` property (which is a default for all non-frozen objects). http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps **/ var privates = Name() return Object.freeze(Object.create(WeakMap.prototype, { has: { value: function has(object) { return 'value' in privates(object) }, configurable: true, enumerable: false, writable: true }, get: { value: function get(key, fallback) { return privates(guard(key)).value || fallback }, configurable: true, enumerable: false, writable: true }, set: { value: function set(key, value) { privates(guard(key)).value = value }, configurable: true, enumerable: false, writable: true }, 'delete': { value: function set(key) { return delete privates(guard(key)).value } } })) } return global.WeakMap = WeakMap })(this)