polyfill_WeakSet.js.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/WeakSet.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/WeakSet.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 WeakSet that takes an object as key.
  31. // All WeakSets look at the same property
  32. var KEY_PROP = Symbol( "WeakSet" );
  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/WeakSet WeakSet}.
  37. * The only way to achieve this is by putting a property on the added value.
  38. * @class module:barejs/polyfill.WeakSet
  39. * @param {*} [_iterable]: Optional: an iterable whose values will be added to the WeakSet.
  40. */
  41. function WeakSet( _iterable )
  42. {
  43. if ( _iterable )
  44. {
  45. for ( var i = 0; i &lt; _iterable.length; ++i )
  46. this.add( _iterable[i] );
  47. }
  48. ObjectPolyfill.defineProperty( this, "_values", { value: [] } );
  49. }
  50. return ( ObjectPolyfill.defineProperties( WeakSet.prototype,
  51. /** @lends module:barejs/polyfill.WeakSet# */
  52. {
  53. _values: { value: null },
  54. /**
  55. * Adds a value to a WeakSet.
  56. * @function
  57. * @param _value The value to add.
  58. * @returns {module:barejs/polyfill.WeakSet} The WeakSet (for chaining)
  59. */
  60. add: { value: function add( _value )
  61. {
  62. if ( !ObjectPolyfill.isObject( _value ) )
  63. throw new TypeError( "Invalid value used as WeakSet value" );
  64. if ( !hOP.call( _value, KEY_PROP ) )
  65. ObjectPolyfill.defineProperty( _value, KEY_PROP, { value: ++nextId } );
  66. if ( this._values.indexOf( _value[KEY_PROP] ) &lt; 0 )
  67. this._values.push( _value[KEY_PROP] );
  68. return this;
  69. } },
  70. /**
  71. * Check if the WeakSet has an entry for the specified _value.
  72. * @function
  73. * @param {object} _value The object to check.
  74. * @returns {boolean} True if the WeakSet contains _value, false otherwise.
  75. */
  76. has: { value: function has( _value )
  77. {
  78. // If there is no WeakSet key property on the object, it has never been used as key for a WeakSet
  79. return ObjectPolyfill.isObject( _value ) &amp;&amp; hOP.call( _value, KEY_PROP ) &amp;&amp; ( this._values.indexOf( _value[KEY_PROP] ) >= 0 );
  80. } },
  81. /**
  82. * Remove the value from the WeakSet
  83. * @function
  84. * @param {object} _value The value to remove.
  85. * @returns {boolean} True if the value was deleted, false otherwise
  86. */
  87. "delete": { value: function _delete( _value )
  88. {
  89. var idx = -1;
  90. if ( ObjectPolyfill.isObject( _value ) &amp;&amp; hOP.call( _value, KEY_PROP ) )
  91. idx = this._values.indexOf( _value[KEY_PROP] );
  92. if ( idx &lt; 0 )
  93. return false;
  94. this._values.splice( idx, 1 );
  95. return true;
  96. } }
  97. } ), WeakSet );
  98. }( require( "./Object" ), require( "../Symbol" ) ) );
  99. </code></pre>
  100. </article>
  101. </section>
  102. </div>
  103. <nav>
  104. <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>
  105. </nav>
  106. <br class="clear">
  107. <footer>
  108. 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)
  109. </footer>
  110. <script> prettyPrint(); </script>
  111. <script src="scripts/linenumber.js"> </script>
  112. </body>
  113. </html>