0

Is there a difference in the type of the object between the two instances of joe below?

var Person = function(name)
{
    this.name = name;
};

var joe = new Person("Joe Bloggs");

console.log(typeof joe);
==========================================

var Person = function(name)
{
    this.name = name;

    return this;
};

var joe = new Person("Joe Bloggs");

console.log(typeof joe);

I'd like to refine my question. I am not really asking how to determine the type of an object in JavaScript, although that too is a question of mine but over here, that's not what I am asking.

Over here, what I am asking is, is:

var Foo = function() { }
var foo = new Foo();

the same as (or how is it different from)

var Foo = function() { return this; }
var foo = new Foo();
Community
  • 1
  • 1
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • When you invoke a function with the `new` keyword, if no explicit return is done in the function `this` will be returned implicitly. But how about you run your code? -- You could have answered your own question if you did. – Igwe Kalu Oct 29 '16 at 10:46
  • @IgweKalu I have run my code and many other snippets. My questions are the result of my rumination. In both the cases, the code outputs `object`. – Water Cooler v2 Oct 29 '16 at 10:47
  • @IgweKalu And in fact, what you say is not accurate. Running some other snippets of mine demonstrates that. I will update my question with them. – Water Cooler v2 Oct 29 '16 at 10:48
  • 2
    Does [Is `return` from constructor necessary when creating object with new](http://stackoverflow.com/q/7064673/4642212) answer your question? How have you executed the code to test for differences? I have executed them in the console and compared their properties. They’re identical. – Sebastian Simon Oct 29 '16 at 10:49
  • @Xufox It does. Thanks much. – Water Cooler v2 Oct 29 '16 at 10:51
  • @IgweKalu I stand corrected. The source of my confusion was my running this snippet. https://jsfiddle.net/7kzbxe5p/ – Water Cooler v2 Oct 29 '16 at 10:51

0 Answers0