Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will new return the named function constructor instance-?

var foo = function () {
    return new moo();
}

var moo = function () {
    return this;
}

If I execute the statement

new foo()

will I actually get an instance of moo? This seems at both the same time obvious and non-intuitive. Functionally, this is what should happen, but at the same time this is not-expected if you didn't know the internals.

EDIT: I realized this seems unintuitive b.c. in Java constructors can not return anything.

This is very similar to the constructor pattern that jquery uses.

like image 488
employee-0 Avatar asked Jul 21 '13 17:07

employee-0


People also ask

What does a constructor function return?

Return from constructors Usually, constructors do not have a return statement. Their task is to write all necessary stuff into this , and it automatically becomes the result. But if there is a return statement, then the rule is simple: If return is called with an object, then the object is returned instead of this .

Which of the following will return the constructor function for an object?

constructor. The constructor property returns a reference to the Object constructor function that created the instance object.

Is it possible to use function constructor to create a new function in JS?

The Function() constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval() .

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.


2 Answers

Yes you will get an instance of moo.

non-intuitiveness is because of the fact that you can return something other than object itself in a javascvipt constructor.This is possible because all functions are in fact objects in js. In languages like java and c# that's not possible, constructor always returns object which constructor belongs to. You can also cannot call contructors without new keyword in such languages. Not returning anything from constructor is same thins as return this; in js thing(assuming it's used a constructor) also adds a little to confusion.

like image 103
Desu Avatar answered Sep 20 '22 10:09

Desu


You are correct you will get an instance of moo

The reason for this being so ambiguous is because whenever new keyword is used, the newly created object's constructor is not executed until 'this' keyword is used. the new object is bound to 'this' keyword.

refer this from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

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

In your example, too a new Object is created but 'this' keyword is not used and hence the constructor for foo is not called and hence the function just ends up returning the moo object.

http://jsfiddle.net/v5aGu/

var foo = function () {
    return new moo();
}

var moo = function () {
    return this;
}

var myFoo = new foo(2);
if(myFoo instanceof moo){
    alert("moo");
}
if(myFoo instanceof foo){
    alert("foo");
}

Edit: to Answer question raised by @Desu

id = 0;

var foo = function(){

}

if(new foo() instanceof foo){
 alert("yes"); //alerts yes
}

JavaScript Constructors 101 :

  1. The default behavior of constructor is to return 'this' if nothing else is returned
  2. If another object is returned from a constructor the newly created object bound to 'this' gets discarded

http://jsfiddle.net/xQVuX/1/

id = 0;

var foo = function(){
}

if(new foo() instanceof foo){
 alert("foo yes"); //alerts foo yes because foo returns this as a default behavior
}

var foo2 = function(){
    var i=new foo();
    return i;
}

if(new foo2() instanceof foo2){
 alert("foo2 yes");// does not alert because foo2 returns another object and the newly created object is discarded
}

var foo3 = function(){
    this.i = 10; 
}

if(new foo3() instanceof foo3){
    alert("foo3 yes"); // alerts foo3 yes because foo3 returns this as a default behavior
}
like image 44
user2580076 Avatar answered Sep 22 '22 10:09

user2580076