I've been reading tutorials for a few weeks now and I've just figured out that when I use the prototype property on a constructor function, the key/value pairs on that prototype property are copied to the proto property of the newly instantiated object.
function F(){};
F.prototype.k = "v";
console.dir(F.k)//logs "undefined"
console.dir(F.prototype.k)//logs "v"
var o = new F;
console.dir(o.k)//logs "v"
So, key "k" is on the prototype property in the constructor and then transfers this to the proto property of newly instantiated object "o" which is why it can be accessed as if it were just a normal key/value pair on the object. Ok, that makes sense to me...but I thought about it and I've seen people use the new keyword for built-in things like String (although this is usually not done)
var s = new String;
the above code is an example of the way a new string item could be created the same way objects can be instantiated based on constructor functions. This made me wonder "is String just a constructor function????!" So I tested this out:
console.dir(String.prototype)
And I got a whole list of exactly the same properties that are attached to s. So, is "String" just a constructor function? The same behavior seems to be true of these items:
console.dir(String.prototype);
console.dir(Number.prototype);
console.dir(Boolean.prototype);
console.dir(Array.prototype);
console.dir(Object.prototype);
console.dir(Function.prototype);
console.dir(Date.prototype);
They all appear to behave exactly the same as constructor functions. They are even all capitalized rather than camelcase. Are they just constructor functions with a few built-in bells and whistles added to them?
new String, ornew Number.prototypeis "copied" into instance?