polyfill_WeakMap.js.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/WeakMap.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: polyfill/WeakMap.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
  20. //
  21. // IBM Watson Analytics
  22. //
  23. // (C) Copyright IBM Corp. 2015
  24. //
  25. // US Government Users Restricted Rights - Use, duplication or
  26. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  27. module.exports = ( function( ObjectPolyfill, Symbol )
  28. {
  29. "use strict";
  30. // The property set by the first keymap that takes an object as key.
  31. // All WeakMaps look at the same property
  32. var KEY_PROP = Symbol( "WeakMap" );
  33. var hOP = Object.prototype.hasOwnProperty;
  34. var nextId = 0;
  35. /**
  36. * Mimics the implementation of a native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap WeakMap}.
  37. * The only way to achieve this is by putting a property on the key object.
  38. * @class module:barejs/polyfill.WeakMap
  39. * @param {any} [_iterable=null] Optional: an iterable whose values will be added to the WeakSet.
  40. */
  41. function WeakMap( _iterable )
  42. {
  43. // Ensure map has no prototype chain (so we can safely use the in operator instead of hasOwnProperty)
  44. ObjectPolyfill.defineProperty( this, "_map", { value: Object.create( null ) } );
  45. if ( _iterable )
  46. {
  47. for ( var i = 0; i &lt; _iterable.length; ++i )
  48. this.set( _iterable[i][0], _iterable[i][1] );
  49. }
  50. }
  51. return ( ObjectPolyfill.defineProperties( WeakMap.prototype,
  52. /** @lends module:barejs/polyfill.WeakMap# */
  53. {
  54. _map: { value: null },
  55. /**
  56. * Adds a new element with a specified key and value to a WeakMap object.
  57. * @function
  58. * @param {object} _key The object to use as key.
  59. * @param _value The value to add.
  60. * @returns {module:barejs/polyfill.WeakMap} The WeakMap (for chaining)
  61. */
  62. "set": { value: function set( _key, _value )
  63. {
  64. if ( !ObjectPolyfill.isObject( _key ) )
  65. throw new TypeError( "Invalid value used as weak map key" );
  66. if ( !hOP.call( _key, KEY_PROP ) )
  67. ObjectPolyfill.defineProperty( _key, KEY_PROP, { value: ++nextId } );
  68. this._map[ _key[KEY_PROP] ] = _value;
  69. return this;
  70. } },
  71. /**
  72. * Get the value for the specified key
  73. * @function
  74. * @param {object} _key The object to use as key.
  75. * @returns The value, or undefined if the key is not known.
  76. */
  77. "get": { value: function get( _key )
  78. {
  79. return this.has( _key ) ? this._map[ _key[KEY_PROP] ] : undefined;
  80. } },
  81. /**
  82. * Check if the WeakMap has an entry for the specified key
  83. * @function
  84. * @param {object} _key The object to use as key.
  85. * @returns {boolean} True if there is an entry for the key, false otherwise
  86. */
  87. has: { value: function has( _key )
  88. {
  89. // If there is no WeakMap key property on the object, it has never been used as key for a WeakMap
  90. return ObjectPolyfill.isObject( _key ) &amp;&amp; hOP.call( _key, KEY_PROP ) &amp;&amp; ( _key[KEY_PROP] in this._map );
  91. } },
  92. /**
  93. * Remove the value for the specified key
  94. * @function
  95. * @param {object} _key The object to use as key.
  96. * @returns {boolean} True if the entry was deleted, false otherwise
  97. */
  98. "delete": { value: function _delete( _key )
  99. {
  100. // Remove the entry from our map (will be a NOOP if there is no property for the key value)
  101. // Note: delete is not called if this.has( _key ) returns false. If the delete statement
  102. // (incorrectly) returns false, we force it to return true using "|| true".
  103. return this.has( _key ) &amp;&amp; ( ( delete this._map[_key[KEY_PROP]] ) || true );
  104. } }
  105. } ), WeakMap );
  106. }( require( "./Object" ), require( "../Symbol" ) ) );
  107. </code></pre>
  108. </article>
  109. </section>
  110. </div>
  111. <nav>
  112. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
  113. </nav>
  114. <br class="clear">
  115. <footer>
  116. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:34 GMT+0200 (W. Europe Daylight Time)
  117. </footer>
  118. <script> prettyPrint(); </script>
  119. <script src="scripts/linenumber.js"> </script>
  120. </body>
  121. </html>