oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.71k stars 2.72k forks source link

Can create and use constructors without `new` operator #8601

Closed aifrim closed 8 months ago

aifrim commented 8 months ago

What version of Bun is running?

1.0.25+a8ff7be64

What platform is your computer?

Darwin 23.3.0 arm64 arm

What steps can reproduce the bug?

Just run this code

function MaybeConstructor(value) {
  if (!this) {
    return new MaybeConstructor(value);
  }

  this.value = value;
}

const a = new MaybeConstructor(42);
const b = MaybeConstructor(42);

console.log(a);
console.log(b);

What is the expected behavior?

Node / Chrome / Safari / Firefox

MaybeConstructor { value: 42 }
undefined

What do you see instead?

Output

MaybeConstructor {
  value: 42,
}
MaybeConstructor {
  value: 42,
}

Additional information

No response

Jarred-Sumner commented 8 months ago

this is sloppy vs strict mode. bun uses ESM by default unless there's a clear sign it should use CJS.

Add module.exports = 123; to the bottom of the file and you'll see it behaves the same as others