polyfill_Function.js.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/Function.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/Function.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. ( function( Object, Function, ObjectPolyfill )
  28. {
  29. "use strict";
  30. /**
  31. * Polyfills for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function Function}.
  32. * @class module:barejs/polyfill.Function
  33. */
  34. /** @lends module:barejs/polyfill.Function# */
  35. var proto = {};
  36. var fnProto = Function.prototype;
  37. /*istanbul ignore else: We test with __ES__ set to 3*/
  38. if ( __ES__ &lt; 5 )
  39. {
  40. var slice = Array.prototype.slice;
  41. /**
  42. * Bind a function to a specific context. Allows for additional prefix arguments to be specified.
  43. * @param {object} _thisArg The context to bind this method to.
  44. * @returns {function} A function that will invoke the original function in the specified context,
  45. * optionally with additional parameters prefixed.
  46. */
  47. proto.bind = function bind( _thisArg /*, ... */ )
  48. {
  49. if ( !ObjectPolyfill.isCallable( this ) )
  50. throw new TypeError( "Bind must be called on a function" );
  51. var bound, fn = this, args = null, Anonymous = function(){};
  52. if ( arguments.length > 1 )
  53. args = slice.call( arguments, 1, arguments.length );
  54. // Apply almost the same trick as Object.create, so if the bound method is used as a
  55. // constructor, we do not change the context and the prototype chain is correct.
  56. // We can't use Object.create since we need the Anonymous type for the instanceof check
  57. // Prototype is undefined for native functions, so check it first.
  58. Anonymous.prototype = fn.prototype /*istanbul ignore next*/|| Object.prototype;
  59. // Only do parameter mangling if required. This optimization provides a significant speed boost
  60. if ( args &amp;&amp; args.length )
  61. {
  62. bound = function()
  63. {
  64. var len = arguments.length;
  65. // Only concat the arguments if there are arguments during invocation (to avoid concat)
  66. // If this bound method is used as a constructor, don't change the context
  67. return fn.apply( this instanceof Anonymous ? this : _thisArg, len ? args.concat( slice.call( arguments, 0, len ) ) : args );
  68. };
  69. }
  70. else
  71. {
  72. bound = function()
  73. {
  74. // If this bound method is used as a constructor, don't change the context
  75. return fn.apply( this instanceof Anonymous ? this : _thisArg, arguments );
  76. };
  77. }
  78. bound.prototype = new Anonymous();
  79. // technically the bound function should have the same length as the function bound, but we can't emulate that
  80. return bound;
  81. };
  82. }
  83. ( function()
  84. {
  85. var toString = fnProto.toString;
  86. /*istanbul ignore next: not invoked since NodeJS has a name property on Function.prototype*/
  87. function name()
  88. {
  89. //jshint validthis:true
  90. if ( this === fnProto )
  91. return "";
  92. var match = toString.call( this ).match( /function\s*([a-zA-Z0-9_$]*)\s*\(/ );
  93. Object.defineProperty( this, "name", { configurable: true, value: ( match &amp;&amp; match[1] ) || "" } );
  94. return this.name;
  95. }
  96. // See if the name property isn't exposed, but we can expose it (and it works)
  97. /*istanbul ignore next: NodeJS has a name property on functions*/
  98. if ( !name.name &amp;&amp; Object.defineProperties &amp;&amp; ( name.call( name ) === "name" ) )
  99. Object.defineProperty( fnProto, "name", { configurable: true, "get": name } );
  100. }() );
  101. ObjectPolyfill.polyfill( Function, null, proto, exports, "Function" );
  102. // End of module
  103. }( Object, Function, require( "./Object" ) ) );
  104. </code></pre>
  105. </article>
  106. </section>
  107. </div>
  108. <nav>
  109. <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>
  110. </nav>
  111. <br class="clear">
  112. <footer>
  113. 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)
  114. </footer>
  115. <script> prettyPrint(); </script>
  116. <script src="scripts/linenumber.js"> </script>
  117. </body>
  118. </html>