Javascript 如何使内部类属性接受父类属性的值?

Javascript 如何使内部类属性接受父类属性的值?,javascript,Javascript,我需要你的帮助 我的问题是如何使内部类属性接受父类属性的值 function drawSvg(element) { this.x= 0; this.y=0; this.strockeColor="#FFF"; this.fillColor= "#000"; this.darwCircle= function() { this.x = parent.x;//here what I have to do to make the x of darwCircle

我需要你的帮助

我的问题是如何使内部类属性接受父类属性的值

function drawSvg(element)
{
  this.x= 0;
  this.y=0;
  this.strockeColor="#FFF";
  this.fillColor= "#000";
  this.darwCircle= function()
  {
       this.x = parent.x;//here what I have to do to make the x of  darwCircle  take the vale of  drawSvg x
  }

}

在我上面的例子中,我不知道应该用什么代码来实现这一点???

一种常见的方法是:

在父作用域中写入

var self = this;
在子作用域中,您可以编写

this.x = self.x; //now you can take the value of the parent
这里有一个完整的例子需要进一步说明

function drawSvg(element)
{
    this.x= 200;
    this.y=0;
    this.strockeColor="#FFF";
    this.fillColor= "#000";
    this.lineWidth =10;
    this.set = function(x, y, strockColor, fillColor, lineWidth )
    {
        this.x= x;
        this.y= y;
        this.strockeColor= strockColor;
        this.fillColor= fillColor;
        this.lineWidth= lineWidth;
    }

    self = this;// look here 

    this.darwCircle= function()
    {
       this.x = self.x+2000; // look here 
    }
}

var d = new drawSvg("ddfds");
var dc = new d.darwCircle();
console.log(dc.x);

strockeColor
可能是一个打字错误……我希望
darwCircle
不是一个类?您打算如何使用此代码?
drawCircle
drawSvg
的一种方法,因此
在两种上下文中都是相同的。您应该注意
新建d.darwCircle()不是一个人应该做的事情。