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