Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What values can a constructor return to avoid returning this?

What are the exact circumstances for which a return statement in Javascript can return a value other than this when a constructor is invoked using the new keyword?

Example:

function Foo () {   return something; }  var foo = new Foo (); 

If I'm not mistaken, if something is a non-function primitive, this will be returned. Otherwise something is returned. Is this correct?

In other words, what values can something take to cause (new Foo () instanceof Foo) === false?

like image 201
Thomas Eding Avatar asked Dec 30 '09 02:12

Thomas Eding


People also ask

What can a constructor return?

No, constructor does not return any value. While declaring a constructor you will not have anything like return type.

Why does the constructor not return any value?

So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.

What can a constructor return C++?

Constructors do not return anything. Constructors are called implicitly while object creation to initialize the object being created.

Can constructor return a value typescript?

You shouldn't return anything in a constructor. A constructor is used to initialize the object.


2 Answers

The exact condition is described on the [[Construct]] internal property, which is used by the new operator:

From the ECMA-262 3rd. Edition Specification:

13.2.2 [[Construct]]

When the [[Construct]] property for a Function object F is called, the following steps are taken:

  1. Create a new native ECMAScript object.
  2. Set the [[Class]] property of Result(1) to "Object".
  3. Get the value of the prototype property of F.
  4. If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3).
  5. If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Object prototype object as described in 15.2.3.1.
  6. Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argument list passed into [[Construct]] as the argument values.
  7. If Type(Result(6)) is Object then return Result(6).
  8. Return Result(1).

Look at steps 7 and 8, the new object will be returned only if the type of Result(6) (the value returned from the F constructor function) is not an Object.

like image 126
Christian C. Salvadó Avatar answered Oct 12 '22 13:10

Christian C. Salvadó


Concrete examples http://jsbin.com/zivivucahi/1/edit?html,js,console,output

/* ECMA 262 v 5 http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf "4.3.2 primitive value member of one of the types Undefined, Null, Boolean, Number, Symbol, or String as defined in clause 6" */  var Person = function(x){   return x;  };   console.log(Person.constructor); console.log(Person.prototype.constructor); console.log(typeof(Person)); console.log(typeof(Person.prototype));  function log(x){   console.log(x instanceof Person);   console.log(typeof x);   console.log(typeof x.prototype); }  log(new Person(undefined)); log(new Person(null)); log(new Person(true)); log(new Person(2)); log(new Person(""));  //returns a function not an object log(new Person(function(){}));   //implementation? //log(new Person(Symbol('%'))); 
like image 42
philn5d Avatar answered Oct 12 '22 14:10

philn5d