1

When I use new to instainciate an instance of a certain class, I got the actual instance. When the constructor function has a return value, the new sentence gives out the actual instance also. However, when the constructor returns itself, I can't get the instance. Instead I get the constructor. I wander what's wrong with this.

Here are my test code fragment:

function foo() {
    this.x = 1;
    return foo;
}
console.log(new foo()); // prints the defination of foo

As we consider, in most situations, it makes no sence to return a function like this. However, why does JS has such a feature? Is there any consideration when designing JS? Or is it just a bug of JS?

Cosmo
  • 836
  • 1
  • 12
  • 27
  • 1
    _"I wander what's wrong with this."_ The biggest thing that's wrong with it is that it makes no sense. Why would you return a constructor from itself? – JLRishe Mar 12 '15 at 10:19

4 Answers4

3

If you return an object from a constructor, the result of the new ... expression will be the object you returned:

function myWackyConstructor() {
  return new Date();
}

var d = new myWackyConstructor();
console.log(d);

If you return a primitive, the result will be the constructed object:

function myWackyConstructor() {
  this.gotAValue = true;

  return 3;
}

var v = new myWackyConstructor();
console.log(v.gotAValue);

Typically, you should not return anything from a constructor:

function myNormalConstructor() {
  this.gotAValue = true;
}

var v = new myNormalConstructor();
console.log(v.gotAValue);

The question is, why are you returning the constructor from itself?

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • A good exception to return something: `if(this.constructor!==TheConstructor){return new this.constructor(arg1,arg2);...}` – HMR Mar 12 '15 at 10:27
  • Thank u! In fact, I don't know why. I was just taking a quiz on JS features and this is one of the questions. But I really also want to know, does this design of JS make sence? Could you please give some explainations or references? :) – Cosmo Mar 13 '15 at 02:49
  • When I'm confused by this, the first issue I considered was: The `String` function can return a JS string, and it will create a `String` instance with `new`. Consider the situation that I need such a function: When I call it as a normal function it returns a specific object, such as an instance of `Date`. Just like the first example you give. On the other hand, when use it as a constructor I want the constructed object instead of the return object. This situation do make sence, I think. – Cosmo Mar 13 '15 at 03:08
1

Your code does raise an eyebrow. It does not make sense to me why you would return a static class in your constructor.

I think if you returned the actual instance it might make more sense to you but it isn't necessary.

example

function foo() {
    this.x = 1;
    return this;
}

var aFooInstance = new foo();

console.log(aFooInstance); // prints a instance of foo

However, you might want to have private variables so you can return an object like so, in this example you can also pass in the data to the constructor.

function foo(x) {

    var _x = x;

    this.getX = function(){ return _x;}      
}

var aFooInstance = new foo(1);

console.log(aFooInstance.getX()); // prints 1

I would suggest reading more on simple class instantiation.

jennas
  • 2,444
  • 2
  • 25
  • 31
  • There's no reason to return `this` from a constructor function, and there's no sense in using `new` if you return something other than the constructed object. – JLRishe Mar 12 '15 at 10:22
  • hey @JLRishe yeah i agree you dont need to `return this`, for clarity i thought it would help the author understand whats happening. However in my 2nd example there is a point, and that is access modifiers.. or 'private variables' :) No code can directly change the value of `_x` on an instance. – jennas Mar 12 '15 at 10:44
  • Yes, but the value returned will not inherit from `foo` and will not have its prototype, so using `new` there is meaningless and confusing. If you want to have private variables and `new`, you can do this: http://jsfiddle.net/rrrjqsq0/ – JLRishe Mar 12 '15 at 10:46
  • @JLRishe yeah thats a better way.. my bad. – jennas Mar 12 '15 at 10:51
0

In JS, when we declare a function as a class and when we create an object of that class, that function gets called first.

Dont return from the function.

0

The Javascript constructor does not need a return. This will work:-

function foo() {
    this.x = 1;
}

var myfoo = new foo();
console.log(myfoo.x);
QuentinUK
  • 2,997
  • 21
  • 20