What is the difference between these two codes?
let user = new function() {
this.name = "John";
this.isAdmin = false;
}
vs
let user = {
name: "John",
isAdmin: false
}
What is the difference between these two codes?
let user = new function() {
this.name = "John";
this.isAdmin = false;
}
vs
let user = {
name: "John",
isAdmin: false
}
The only difference is that the first
user, created vianew, has an internal prototype of a (nearly) empty object, and that empty object inherits fromObject.prototype:Technically, the object isn't entirely empty, it has a non-enumerable
constructorproperty pointing to the functionThis nearly empty object is the
.prototypeproperty of thefunction- but since that prototype object doesn't have any properties on it, it looks empty. If you had put properties on the function's.prototypeobject, these would be visible on the intermediate object:The user from an object literal, on the other hand, inherits directly from
Object.prototype: