Every function in JavaScript is itself an object. So Person.name retrieves the name property from the function itself, which was never set; hence undefined.
You can try this my setting it directly:
Person.name = "John";
Person.name; // "John"
When assigning a property from within the constructor via
this.name = "Allen Kim";
you're setting the property on that instance. When you then instantiate an object with:
var me = new Person();
your constructor will add the name property to me, since this referes to the object being created.
Here are the basic steps taken by the JavaScript engine when calling a constructor function with the new keyword:
- Calls the constructor with
this set to a new clean object.
- Sets the internal
[[Prototype]] property of your new object to the constructor's prototype (which, in some implementations, is then available through __proto__).
- Sets up the
constructor property of your new object as a reference to the constructor function (so instead of the non-standard me.__proto__, you could access the prototype via me.constructor.prototype).
- Returns said object.
Note that this is a very basic explanation. There are many other things not included here, but this should give you the gist of it.