我需要你的帮助。
我的问题是如何使内部类属性采用父类属性的值
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
}
}
在我上面的例子中,我不知道我应该做什么代码?
答案 0 :(得分:1)
执行此操作的常用方法包括:
父范围写中的
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);