2

So let's say I have a javascript function

function A(){
    return function(){
        console.log('something');
        return new B();
    }
}

Is calling this using new A(); any different than A();?

Jim Jones
  • 2,568
  • 6
  • 26
  • 43
  • 6
    Related - [What is the 'new' keyword in JavaScript?](http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript) Dupe? – Lix Feb 24 '14 at 09:36

3 Answers3

2

Since that function has an explicit return and makes no use of this, there is no difference in the result of running it with or without new.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Agree - there would be no difference in the result. But, one would think that using `new A()` would actually create a new object that would subsequently be garbage collected when something else was returned from the function so the `new A()` would probably perform worse. A [jsPerf](http://jsperf.com/new-perf) confirms that `new A()` is 10%-80% slower depending upon browser. – jfriend00 Feb 24 '14 at 09:43
  • "Since that function has an explicit `return` and makes no use of `this`". Could you tell us what you mean by "that function"? (`A()` or the anonymous function?). Also I think the result will be equivalent regardless of the use of `this` (See my answer for the explanation and please let me know if I'm missing something). – sampathsris Aug 16 '14 at 18:01
1

When you call the function without "new", what is it that you suspect "this" is pointing to? It'll be "window." Updating that is slower than updating the freshly-built new object you'll be using when you invoke it with "new".

Furquan Khan
  • 1,586
  • 1
  • 15
  • 30
  • Well there actually is no 'this' in function A, sorry I forgot to add it too my post but the reason I'm wanting to use new is because the nested function calls new on another function, and I find it a little easier to understand that a functions return an object if a put new there – Jim Jones Feb 24 '14 at 09:47
0

Others have given answers about the result (is the use of new equivalent to not using it or not?), but let me explain why following two results are equivalent:

var no_new = A();
var with_new = new A();

The relevant part of the ECMAScript is section 13.2.2 (I will omit and simplify some information and adapt the contents of that section to suit this answer). This is what happens when new A() is executed:

  1. A new object is created.
  2. The prototype of function A is examined:
    1. If the prototype of A is an object, newly created object's prototype is set to A's prototype.
    2. If not, newly created object's prototype is set to the default prototype of Object.
  3. Function A() is called, and the result is examined.
    1. If the result is an object, the result will be the return value of new.
    2. Else the newly created object will be the return value of new.

In your case, the execution will take the path 3.1, aka new operator will return a reference to the inner anonymous function, same as the function call without new.

So, both variables no_new and with_new will have a reference to a function in the following form:

function () {
    console.log('something');
    return new B();
}

(Note that no_new and with_new are equivalent but not identical. They do not refer to the same object: JSFiddle)

function A() {
    return function(){
        console.log('something');
        return new B();
    }
}

function B() {
}

var no_new = A();
var with_new = new A();

alert(no_new);
alert(with_new);

var are_they_same = (no_new === with_new);
alert(are_they_same);
sampathsris
  • 21,564
  • 12
  • 71
  • 98