es.function.has-instance.js 974 B

12345678910111213141516171819202122
  1. 'use strict';
  2. var isCallable = require('../internals/is-callable');
  3. var isObject = require('../internals/is-object');
  4. var definePropertyModule = require('../internals/object-define-property');
  5. var getPrototypeOf = require('../internals/object-get-prototype-of');
  6. var wellKnownSymbol = require('../internals/well-known-symbol');
  7. var HAS_INSTANCE = wellKnownSymbol('hasInstance');
  8. var FunctionPrototype = Function.prototype;
  9. // `Function.prototype[@@hasInstance]` method
  10. // https://tc39.es/ecma262/#sec-function.prototype-@@hasinstance
  11. if (!(HAS_INSTANCE in FunctionPrototype)) {
  12. definePropertyModule.f(FunctionPrototype, HAS_INSTANCE, { value: function (O) {
  13. if (!isCallable(this) || !isObject(O)) return false;
  14. var P = this.prototype;
  15. if (!isObject(P)) return O instanceof this;
  16. // for environment w/o native `@@hasInstance` logic enough `instanceof`, but add this:
  17. while (O = getPrototypeOf(O)) if (P === O) return true;
  18. return false;
  19. } });
  20. }