Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is RETURN from constructor necessary when creating object with new

if I have function like this:

function Apple(){
    this.color = "green";
    return this;
}

When creating object like this:

var my_obj = new Apple();

is that line return this; necessary and/or is it valid by language reference?

like image 646
Marek Sebera Avatar asked Aug 15 '11 12:08

Marek Sebera


People also ask

Do you need a return in an constructor?

No, constructor does not have any return type in Java. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class.

Does a constructor return an object?

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

What should be returned from a constructor?

A constructor doesn't return anything.


Video Answer


2 Answers

No, returning this is not necessary, but it is valid. If the returned value is an object, new will return that object instead of the newly created object.

See points 11.2.2 and 13.2.2 of ECMAScript 5:

The new operator calls the internal [[Construct]] method on the constructor (usually a function):

11.2.2 The new Operator

The production NewExpression : new NewExpression is evaluated as follows:

  1. Let ref be the result of evaluating NewExpression.
  2. Let constructor be GetValue(ref).
  3. If Type(constructor) is not Object, throw a TypeError exception.
  4. If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
  5. Return the result of calling the [[Construct]] internal method on constructor, providing no arguments (that is, an empty list of arguments).

The [[Construct]] internal method of functions is described in point 13.2.2:

13.2.2 [[Construct]]

When the [[Construct]] internal method for a Function object F is called with a possibly empty list of arguments, the following steps are taken:

  1. Let obj be a newly created native ECMAScript object.
  2. Set all the internal methods of obj as specified in 8.12.
  3. Set the [[Class]] internal property of obj to "Object".
  4. Set the [[Extensible]] internal property of obj to true.
  5. Let proto be the value of calling the [[Get]] internal property of F with argument "prototype".
  6. If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
  7. If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.
  8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
  9. If Type(result) is Object then return result.
  10. Return obj.
like image 64
Arnaud Le Blanc Avatar answered Oct 16 '22 04:10

Arnaud Le Blanc


It is not necessary, a constructor automatically returns the newly created object.

About explicitly returning a value from constructor this page has good information: JavaScript: Constructor Return Value

Quote:

If a constructor function returns nothing, null, or any atomic / non-object value then said value is ignored and the newly created object reference is given back to the caller. For example, a return value of 0 (zero) from a constructor function will be ignored.

and

...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.

like image 45
nobody Avatar answered Oct 16 '22 03:10

nobody