简体   繁体   English

Javascript原型继承和对象构造函数

[英]Javascript prototype inheritance and Object Constructor

I have following piece of code for learning JS. 我有以下用于学习JS的代码。

function CircleArea(x)
{
   this.x = x;
}

CircleArea.prototype = 
{  
   area: function () {
       return 22 / 7 * this.x * this.x;
   }
};

var CalArea = new CircleArea(7);
if( CalArea.constructor === CircleArea.prototype.constructor)
{
   alert(CalArea.area());
}

I decoupled the inheritance chain by assigning an object literal to CircleArea.prototype and then defined CalArea object using CircleArea constructor. 我通过将对象文字分配给CircleArea.prototype来取消继承链的耦合,然后使用CircleArea构造函数定义CalArea对象。 Now both CalArea.constructor and CircleArea.prototype.constructor are basically Object constructors rather than CircleArea constructor but when I called CalArea.area() inside alert function this.x obtains 7 as its value whereas value 7 is passed as an argument to CircleArea constructor not to Object constructor to which CalArea.constructor and CircleArea.prototype constructors refer now. 现在,CalArea.constructor和CircleArea.prototype.constructor基本上都是对象构造函数,而不是CircleArea构造函数,但是当我在警报函数中调用CalArea.area()时,this.x获得7作为其值,而值7作为参数传递给CircleArea构造函数而不是CalArea.constructor和CircleArea.prototype构造函数现在引用的Object构造函数。

I'm not sure what you mean by "decoupled the inheritance chain" but to me, what you are experiencing is the expected behavior. 我不确定“分离继承链”是什么意思,但是对我来说,您正在体验的是预期的行为。

You call area() on an instance of CircleArea . 您在CircleArea实例上调用area()

area() uses this.x . area()使用this.x

this in that context is your instance of CircleArea that has x=7 . this在这方面是你的实例CircleAreax=7

So your calculation is 22 / 7 * 7 * 7 所以你的计算是22 / 7 * 7 * 7

If you are expecting something different, could you explain what you expect and why you expect it? 如果您期望有所不同,能否解释您期望什么以及为什么期望它?


I am guessing that your confusion stems from this which is set to the object instance that a function is called on, so calling CalArea.area() means that this is set to CalArea so this.x is the same as CalArea.x 我猜您的困惑源于this ,后者被设置为调用函数的对象实例,因此调用CalArea.area()意味着将this设置为CalArea因此this.xCalArea.x相同

When you assign an object to prototype (CircleArea.prototype = {area: fun..}) you just dispatch default CircleArea.prototype hence CircleArea.prototype.constructor dispatched too. 将对象分配给原型时(CircleArea.prototype = {area:fun ..}),您只需调度默认的CircleArea.prototype,因此也调度了CircleArea.prototype.constructor。 But CircleArea still is constructor when you use operator new it returns an object instance of CircleArea ({x: argument, proto : { area: fun, proto :...}}). 但是,当您使用new运算符时,CircleArea仍然是构造函数,它返回CircleArea的对象实例({x:自变量, proto :{area:fun, proto :...}})。 You just can't use new CalArea.constructor() but can new CircleArea(). 您只是不能使用新的CalArea.constructor(),而可以使用新的CircleArea()。 new CalArea.constructor() returns the Object instance 新的CalArea.constructor()返回Object实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM