Both of the following constructor functions work the same (as far as I have been able to tell) when I invoke them as:
var x2z = new xyz('ralph');
whether I write a constructor function like this:
function xyz(name) {
return {
name: name,
x:1,
get y() {return this.x+1},
get z() {return this.y+1}
}
}
or if I had declared it:
function xyz(name) {
this.name = name;
this.x = 1;
Object.defineProperties(this, {
"y": {"get": function() {return this.x+1}}
}),
Object.defineProperties(this, {
"z": {"get": function() {return this.y+1}}
})
}
The second form is the one that is documented everywhere. But why does the first one work? Is the new keyword just ignored and the object literal returned?
How are the two related to one another? If someone could point me to a reference that would be great; I can find a lot of stuff on objects but I haven't found anything on the first form.
I put together a simple jsperf and the literal version is slightly faster, but not enough to use it if it's just an oddity. (jsperf example). But it seems more straightforward than the more typically used constructor that refers to "this".