Javascript - How does an object know a function in its class's prototype?
So..Let's say I have the following code snippet
function Rectangle(w,h){
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function(){ return this.wid开发者_Python百科th * this.height; }
var object2 = new Rectangle(10,5);
object2.area();
So...object2 is invoking area() method, which is defined in Rectangle's prototype.
How does object2 know existence of area() method? I guess there must be something like...pointer in object2 that points to Rectangle's prototype?
This is implementation specific, but in Firefox and Webkit, the pointer is in the object's __proto__
property.
See MDC reference.
Another link that explains in more detail the internals of property lookups.
精彩评论