0

Is there some functional difference between using:

var Hello = function() {
    this.a = 1;
    this.b = 2;
};

hello = new Hello();
hello.a; // 1
hello.b; // 2

and the most used way where you use:

function Hello() {
  this.a = 1;
  this.b = 2;
}

hello = new Hello();
hello.a; // 1
hello.b; // 2

As far as I understand, the code inside function() is a constructor, and nothing more. Both have Hello.prototype created upon their definition, the only difference is hoising. Is there something I'm missing?

EDIT: In this question, one person pointed that using the function expression version, one.constructor.name will not be defined which I validated is true. Another person pointed that __proto__ of the newly created object in the function expression points to Object.prototype but I can't verify this in Chrome. Maybe it's true for earlier versions of IE etc.?

Community
  • 1
  • 1
daremkd
  • 7,554
  • 4
  • 34
  • 60
  • The second one is defined at the top of the scope – Hacketo Jan 21 '16 at 15:51
  • Its not clear that you are calling second function at all, probably you should edit the question with specifying what are you comparing. – Dawid Pura Jan 21 '16 at 15:55
  • @Blazemonger have you read the actual question? This is not about the difference between function declarations/expression, it's about the difference between using them with the NEW keyword. – daremkd Jan 21 '16 at 15:55
  • You say in your question that the only difference appears to be hoisting. Why do you suspect there are other differences? – Blazemonger Jan 21 '16 at 15:57
  • I can't see any other differences but this answer is too obvious :-( – Dawid Pura Jan 21 '16 at 15:59
  • People in the question you marked as duplicate pointed 2 already. The first being that in some versions (the answerer didn't specify in which, so not sure how true the answer is), __proto__ of the function expression created object points to Object.prototype. The second answer was that (using this example) a.constructor.name points to nothing, compared to when you create it with a function declaration. I suspect there are more differences. There haven't been a thread on SO about this specifically. – daremkd Jan 21 '16 at 16:00
  • You can't say "I suspect there are more differences" and expect us to come up with them. If you have no *evidence* that there are other differences, then this is a hypothetical question and [unsuitable for SO.](http://stackoverflow.com/help/dont-ask) – Blazemonger Jan 21 '16 at 16:02
  • My question was: What are the differences when you use function declaration vs. functional expression with "new", besides the obvious ones. Using the logic you provided, most of the questions on SO are hypothetical because an answer might not exist... – daremkd Jan 21 '16 at 16:05
  • Please re-open the question because it's far from hypothetical, it's a specific question "Differences between function declaration vs. expressions when using with "new" operator" which have or may not have an answer, which is opposite of the definition of a hypothetical question. – daremkd Jan 21 '16 at 16:08
  • "*Another person pointed that `__proto__` of the newly created object in the function expression points to `Object.prototype`*" - where exactly? (can you link the answer/comment?) This is blatant nonsense. – Bergi Jan 21 '16 at 16:28
  • I think you found it, because I saw you comment on it 2 mins after you wrote this comment :) – daremkd Jan 21 '16 at 16:40

0 Answers0