开发者

Javascript - How does an object know a function in its class's prototype?

开发者 https://www.devze.com 2023-02-14 08:11 出处:网络
So..Let\'s say I have the following code snippet function Rectangle(w,h){ this.width = w; this.height = h;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号