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?
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?
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:
- Let ref be the result of evaluating NewExpression.
- Let constructor be GetValue(ref).
- If Type(constructor) is not Object, throw a TypeError exception.
- If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
- 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:
- Let obj be a newly created native ECMAScript object.
- Set all the internal methods of obj as specified in 8.12.
- Set the [[Class]] internal property of obj to "Object".
- Set the [[Extensible]] internal property of obj to true.
- Let proto be the value of calling the [[Get]] internal property of F with argument "prototype".
- If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
- 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.
- 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.
- If Type(result) is Object then return result.
- Return obj.
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.
It is not necessary.
The function will implicitly return a new Object
when instantiated with the new
operator. this
will refer to that new object in that context.
Invalid returns (such as primitives or non new objects) will still return the standard this
.
You can override it by returning a new different object.
var a = function() {
return new Number(42);
},
b = new a; // typeof b === 'number'.
jsFiddle.