1

I'm getting a bit confused with the new keyword in JavaScript. Take the following example

function A (value) {
    this.a = value;
}

function B (value) {
    return new A (value);
}

console.log (    B (0)); // { a: 0 }
console.log (new B (0)); // { a: 0 }

I want to be able to create a new instance of "A" without having to use "new". For that I have "B()", however, when I call "new B()" it appears to do the same thing as "B()", as though "new" was ignored. In both cases, instanceof equals "A". What exactly is going on?

Dave
  • 7,283
  • 12
  • 55
  • 101
  • i have answered this for many times^^ and others did, too http://stackoverflow.com/questions/31029318/difference-in-declaring-javascript-objects/31029689#31029689 – messerbill Jul 23 '15 at 13:32
  • I'm not sure if you can actually prescribe `new X()` behavior to a function that has `return *value*` in it. The ability to write `new B()` is kind of just allowing different coding styles instead of requiring someone to write it just as `B()` with knowledge of implementation. – Katana314 Jul 23 '15 at 13:34
  • 1
    @messerbill Did you paste the wrong link ? – Denys Séguret Jul 23 '15 at 13:34
  • no, in that link it is explained how JavaScript Objects work. I think if you understand this, the OP's problems are solved. Thats why i did not post it as an answer – messerbill Jul 23 '15 at 13:35
  • I reopened because it makes no sense in my opinion to close with a general broad answer when there's a specific question. You might close again if you don't agree. – Denys Séguret Jul 23 '15 at 13:36
  • 1
    @Denys [The duplicate](http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript) answered *how `new` works*, which is exactly the issue the OP is obviously not understanding. I think that was a pretty good fit... – deceze Jul 23 '15 at 13:38
  • @messerbill I understand how objects work, I just don't understand what's happening with the double new scenario. It's as though new get's ignored whenever there's a return. – Dave Jul 23 '15 at 13:39
  • @Dave That's (almost) [exactly right](http://stackoverflow.com/a/3658673/476). – deceze Jul 23 '15 at 13:40
  • @Dave doesn't my answer make it clear to you ? – Denys Séguret Jul 23 '15 at 13:40
  • @Dave i think the answer should be your solution in this case... – messerbill Jul 23 '15 at 13:41
  • Yeah it is, sorry I'm just not keeping up with the discussion here. – Dave Jul 23 '15 at 13:42

1 Answers1

5

From the MDN on new:

The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)

This extract shows you that

  • what you do with new B isn't "normal" (there's just no reason to do that)
  • the value explicitely returned by B is also returned by new B

You won't be confused if you stick to the normal use of new: passing it a normal constructor, that is a function which doesn't return anything but simply initialize its context (this).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Okay so in essence, if I return an object or function inside the constructor, that's what gets used for the new. If I return something else like a number it gets ignored and new uses the constructor. – Dave Jul 23 '15 at 13:47
  • @Dave That's globally right. In both cases the code in the function is executed and if you use `new` the context (`this`) is a new instance of `B` (which is or not returned, depending on whether you add that `return new A` or not). – Denys Séguret Jul 23 '15 at 13:52