What Is Returned from a Constructor

What if I write return statement in constructor?

Yes, using return statements in constructors is perfectly standard.

Constructors are functions that do not return a value. The family of functions that do not return a value consists of: void functions, constructors and destructors. It is stated in 6.6.3/2 in the C++ standard. The very same 6.6.3/2 states that it is illegal to use return with an argument in a function that does not return a value.

6.6.3 The return statement

2 A return statement without an
expression can be used only in
functions that do not return a value,
that is, a function with the return
type void, a constructor (12.1), or a
destructor (12.4). A return statement
with an expression of non-void type
can be used only in functions
returning a value; the value of the
expression is returned to the caller
of the function.

Additionally, 12.1/12 states that

12.1 Constructors

12 No return type (not even void) shall
be specified for a constructor. A
return statement in the body of a
constructor shall not specify a return
value.

Note, BTW, that in C++ it is legal to use return with an argument in a void function, as long as the argument of return has type void

void foo() {
return (void) 0; // Legal in C++ (but not in C)
}

This is not allowed in constructors though, since constructors are not void functions.

There's also one relatively obscure restriction relevant to the usage of return with constructors: it is illegal to use return in function-try-block of a constructor (with other functions it is OK)

15.3 Handling an exception

15 If a return statement appears in a
handler of the function-try-block of a
constructor, the program is ill formed.

What is returned from a constructor?

I found this great link:

JavaScript: Constructor Return Value

The second piece of magic eluded to above is the ability for a
constructor to return a specific, possibly pre-existing object, rather
than a reference to a new instance. This would allow you to manage the
number of actual instances yourself if needed; possibly for reasons of
limited resources or whatnot.

var g_deebee = new Deebee();
function Deebee() { return g_deebee; }
var db1 = new Deebee();
var db2 = new Deebee();
if (db1 != db2)
throw Error("JS constructor returned wrong object!");
else console.log('Equal');

How does constructor return if it doesn't have any return type?

Constructors don't return anything. A constructor simply initializes an instance.

A new instance creation expression

new SomeExample();

produces a reference to a new instance of the specified class

A new class instance is explicitly created when evaluation of a class
instance creation expression (§15.9) causes a class to be
instantiated.

and invokes the corresponding constructor to initialize the created instance

Just before a reference to the newly created object is returned as the
result, the indicated constructor is processed to initialize the new
object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then
    evaluate the arguments and process that constructor invocation
    recursively using these same five steps. If that constructor
    invocation completes abruptly, then this procedure completes abruptly
    for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If
    this constructor is for a class other than Object, then this
    constructor will begin with an explicit or implicit invocation of a
    superclass constructor (using super). Evaluate the arguments and
    process that superclass constructor invocation recursively using these
    same five steps. If that constructor invocation completes abruptly,
    then this procedure completes abruptly for the same reason. Otherwise,
    continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable
    initializers to the corresponding instance variables, in the
    left-to-right order in which they appear textually in the source code
    for the class. If execution of any of these initializers results in an
    exception, then no further initializers are processed and this
    procedure completes abruptly with that same exception. Otherwise,
    continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the
    same reason. Otherwise, this procedure completes normally.

What is the return value of an ES2015 constructor function?

According to MDN's reference on the new operator:

When the code new Foo(...) is executed, the following things happen:

  1. A new object is created, inheriting from Foo.prototype.
  2. The constructor function Foo is called with the specified arguments, and with this bound to the newly created object. new Foo is equivalent to new Foo(), i.e. if no argument list is specified, Foo is called without arguments.
  3. 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.)

(Emphasis mine.) So it seems that the return value of the constructor function is an object, more specifically an instance of the class.

Further research showed that the return value of the constructor can be overridden by explicitly returning any object from within the constructor function (though non-object return values will be ignored, in which case the default of the newly created object reference is used.) I could not find anything to indicate that ES2015 constructors are any different than constructors prior to ES2015 classes.

This Stack Overflow post was very helpful in researching the above, particularly this article it contained a link to, though since constructor did not appear in the code in either the question nor the article, with my limited understanding of constructors that I had going into researching this they did not seem at first to answer my question. Hopefully this may clarify for others in the same situation.

Edit: The veracity of MDN's information was called into question in comments, so I researched a more definitive source. The ECMA specification says of constructors:

A constructor is an object that supports the [[Construct]] internal method.

The [[Construct]] internal method is then defined as follows:

Signature: (a List of any, Object) → Object

Description: Creates an object. Invoked via the new or super operators. The first argument to the internal method is a list containing the arguments of the operator. The second argument is the object to which the new operator was initially applied. Objects that implement this internal method are called constructors. A function object is not necessarily a constructor and such non-constructor function objects do not have a [[Construct]] internal method.

The spec states that in describing signatures such as the one above, the following convention is used: "If an internal method explicitly returns a value, its parameter list is followed by the symbol “→” and the type name of the returned value."

So, to summarize, based on the ECMA specification as quoted above, yes, constructor functions definitively do in fact return objects.

Is it true that constructor returns an object?

No, a constructor initialises an object that's already been created. It doesn't return an object. Your tutor is wrong.

You don't need to write return; inside a constructor, unless you're returning before the end of the code.

C++ error returning value from a constructor

Constructors and destructors can't return anything. Perhaps you can store the return value in a public property of the class the constructor is for, and provide an alternate function to get that property.



Related Topics



Leave a reply



Submit