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  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/

相关文章:

javascript - 使用 ng-options 动态添加禁用选项

javascript - 使用class和document.documentElement检测js

javascript - backbone.js 对不再使用的模型做了什么

javascript - 如果电话号码已注册,则显示错误消息

javascript - 我如何改进和缩短以下比较两个数组并返回它们之间的对称差异的算法?

javascript - Google Apps 脚本 - 将 UrlFetch 响应转换为数组

javascript - 如何在 Angular 2 中返回 forEach?

javascript - 使用 Netlify lambda 函数从 GatsbyJS 站点发送电子邮件

javascript - 在 jQuery 中使用字符串调用嵌套对象函数

javascript - Vue SPA 在嵌套 Promise 中检索错误代码(不是 200)的状态代码