JavaScript: How many objects are allocated when returning an object literal from a class constructor?

1 week ago 4
ARTICLE AD BOX

Let's say I have this code:

class MyClass { constructor() { return {}; } } const x = new MyClass(); // how many objects are instantiated with this?

I know that variable x just holds a reference to the empty object literal {} returned from the constructor. But, for the purposes of knowing what is actually allocated behind the scenes in JavaScript, does JavaScript instantiate two objects, and instantly throw one away?

Read Entire Article