作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要你的帮助。
我的问题是如何使内部类属性采用父类属性的值
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);
关于javascript - 如何让内部类属性采用父类属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23779900/
我是一名优秀的程序员,十分优秀!