Files
scriptc/tests/corpus/5817-constructor-function-instanceof.cjs
T
Chris Tate 447f56e5c3 Share defaultMaxListeners with the typed EventEmitter, link instanceof alone, and clean up a pipe once
- EventEmitter.defaultMaxListeners on the untyped constructor is Node's
  accessor over the default the typed EventEmitter reads: assignments
  validate as validateNumber(arg, 'defaultMaxListeners', 0) does, and
  getMaxListeners() of emitters without their own limit answers the new
  value.
- dyn.builtinInstanceOf and dyn.functionInstanceOf link the
  constructor-function unit and the units it calls (dyn-invoke, inspect,
  symbol, emitter), so a program whose only use is `this instanceof F`
  links.
- The legacy pipe's cleanup runs once: when the destination emits
  'close' from end(), the cleanup still queued on the source's 'end'
  finds the listeners removed and emit('end') answers true.
2026-10-20 11:28:28 -05:00

16 lines
651 B
JavaScript

// `this instanceof F` in a constructor function, in a program that uses no
// EventEmitter or stream constructor value: the call-without-new idiom
// returns a new instance, and instanceof follows the function's prototype.
"use strict";
function Glob(pattern) {
if (!(this instanceof Glob)) return new Glob(pattern);
this.pattern = pattern;
}
Glob.prototype.describe = function () {
return `glob ${this.pattern}`;
};
const called = Glob("*.ts");
const constructed = new Glob("*.js");
console.log(called.describe(), constructed.describe());
console.log(called instanceof Glob, {} instanceof Glob, Object.create(Glob.prototype) instanceof Glob);