es.function.has-instance.js 1.0 KB

1234567891011121314151617181920212223
  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 makeBuiltIn = require('../internals/make-built-in');
  8. var HAS_INSTANCE = wellKnownSymbol('hasInstance');
  9. var FunctionPrototype = Function.prototype;
  10. // `Function.prototype[@@hasInstance]` method
  11. // https://tc39.es/ecma262/#sec-function.prototype-@@hasinstance
  12. if (!(HAS_INSTANCE in FunctionPrototype)) {
  13. definePropertyModule.f(FunctionPrototype, HAS_INSTANCE, { value: makeBuiltIn(function (O) {
  14. if (!isCallable(this) || !isObject(O)) return false;
  15. var P = this.prototype;
  16. if (!isObject(P)) return O instanceof this;
  17. // for environment w/o native `@@hasInstance` logic enough `instanceof`, but add this:
  18. while (O = getPrototypeOf(O)) if (P === O) return true;
  19. return false;
  20. }, HAS_INSTANCE) });
  21. }