Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript `new` keyword on function returning array

I was experimenting with the new keyword and I can't find an explanation for this behavior. Let's say we have a function returning an integer:

(In firebug)

>>> function x() { return 2; }
>>> x()
2
>>> new x()
x { }

But if the function returns an array :

>>> function y() { return [2]; }
>>> y()
[2]
>>> new y()
[2]

Why is that ?

like image 321
slaphappy Avatar asked Jun 27 '11 09:06

slaphappy


People also ask

What happens when you call a function with the new keyword?

On calling the constructor function with 'new' operator, the following actions are taken: A new empty object is created. The new object's internal 'Prototype' property (__proto__) is set the same as the prototype of the constructing function. The 'this' variable is made to point to the newly created object.

Can a function return an array JavaScript?

JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.

What new keywords return?

Explanation: The new keyword instantiates the class using the parameterized constructor and returns the memory reference of the new object to initialize the variable obj. Using the obj variable, we can access the members of the new object as shown in the output.

What does the new keyword return if the function does not return an object?

It returns the newly created object, unless the constructor function returns a non- null object reference. In this case, that object reference is returned instead.


2 Answers

The new operator has an interesting behavior: It returns the object created by the operator unless the constructor function returns a different object. Any non-object return value of the constructor function is ignored, which is why when you return 2 you don't see this.

Here's what happens when you say new x():

  1. The interpreter creates a new blank object.
  2. It sets the object's underlying prototype to x.prototype.
  3. It calls x with this set to the new object.
  4. In the normal case, x doesn't return anything and the result of the new expression is the new object created in step 1. But, if x returns a non-null object reference, then that object reference is the result of the new expression rather than the object created in step 1. Any other kind of return value (null, primitive numbers, primitive strings, undefined, etc.) is ignored; it has to be a non-null object reference to take precedence over the object new created.

This special treatment given to object references by the new operator lets you substitute a different object for the one new created. This can be handy in some limited situations, but the vast majority of the time, a function designed to be used with new (called a constructor function) shouldn't return anything at all.

For some light reading (hah!), this is covered by Section 13.2.2 ("[[Construct]]") of the specification (HTML; PDF), which is referenced by Section 11.2.2 ("The new operator").

like image 67
T.J. Crowder Avatar answered Sep 19 '22 16:09

T.J. Crowder


Because an array is an object but 2 is not.

If you call a function with the new keyword, it has to return an object. If you don't do that explicitly, it automatically returns this (which is an empty object that inherits from funcName.prototype).

like image 37
Felix Kling Avatar answered Sep 21 '22 16:09

Felix Kling