如何使内部类的属性采用父类属性的值?

问题描述:

我需要你的帮助。如何使内部类的属性采用父类属性的值?

我的问题是如何使内部类属性有父类的属性

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

'strockeColor'可能是一个错字... –

+0

我希望'darwCircle'不是一堂课?你打算如何使用这些代码? – Bergi

+0

'drawCircle'是'drawSvg'的一种方法,所以'this'在两个上下文中都是相同的。 –

这样做的一种常见的方式是,包括:

父范围

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); 
+0

你应该注意到'new d.darwCircle();'不应该做任何事情。 – Bergi