1

I'm just beginning to learn JavaScript. It seems to me that

B = new A();

is the same as,

B = Object.create(A.prototype);
A.call(B);

Is my understanding correct?

jcai
  • 3,138
  • 3
  • 17
  • 32

1 Answers1

2

Is my understanding correct?

Yes, that's basically what new does.

But you might need to consider some edge cases - when A does return an object, the new invocation will yield that and not the this value. For the exact steps, see MDN or How does the "this" keyword work?.

Also, for some special constructors (not the standard user-declared functions) the behaviour can differ a lot between calls with and without new. The built-in constructors (Array etc.) are prominent examples.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164