Every time a function() {} is evaluated, it creates a new function object. Therefore, in #1 all of the User objects are sharing the same getName and getAge functions, but in #2 and #3, each object has its own copy of getName and getAge. All of the different getName functions all behave exactly the same, so you can’t see any difference in the output.
The {…} shorthand is a constructor. When evaluated, it constructs a new “Object” with the given properties. When you run “new User(…)”, it constructs a new “User”. You happen to have created an Object with the same behavior as a User, but they are of different types.
Response to comment:
You can’t, directly. You could make a function that creates a new object as per #3. For example:
function make_user(name, age) {
return {
name: name,
age: age,
getName: function() { return name; },
getAge: function() { return age; },
};
}
var user = make_user("Joe", "18");