繁体   English   中英

将父范围添加到匿名函数-Angular 2中的JavaScript

[英]Adding parent scope to anonymous function - JavaScript to Angular 2

我试图在Angular 2项目中包含一个旧的JavaScript模块,但是在访问父范围时遇到了问题。

let testString = "Parent Scope Accessed!";

Object.keys(data).forEach((function(Key, Index) {
    if(filter.type == 'parameter') {
      Object.keys(dirArray).forEach(function(dirKey, dirIndex) {
        linkArray = dirArray[dirKey];
        if(filter.dir == 2) {  //Direction filter
          Object.keys(linkArray).forEach(function(linkKey, linkIndex) {
            if(filter.type != 'sub')) {
              dataObject = linkArray[linkKey];

              //ERROR with scoping occurs below.  Need to add parent scope.
              console.log(this.testString);
            }
          });
        }
      });
    }
}));

我试图做这样的事情:

let testString = "Parent Scope Accessed!";

Object.keys(data).forEach((function(Key, Index) => {
    if(filter.type == 'parameter') {
      Object.keys(dirArray).forEach(function(dirKey, dirIndex) => {
        linkArray = dirArray[dirKey];
        if(filter.dir == 2) {  //Direction filter
          Object.keys(linkArray).forEach(function(linkKey, linkIndex) => {
            if(filter.type != 'sub')) {
              dataObject = linkArray[linkKey];

              //ERROR with scoping occurs below.  Need to add parent scope.
              console.log(this.testString);
            }
          });
        }
      });
    }
}));

但这带来了一系列全新的问题,但是至少IDE指示已添加了父作用域。 我假设我没有正确使用'=>'语法。 有一个更好的方法吗?

定义函数时,放下function词并仅使用粗箭头=>

 let testString = "Parent Scope Accessed!";

Object.keys(data).forEach(((Key, Index)=> {
    if(filter.type == 'parameter') {
      Object.keys(dirArray).forEach((dirKey, dirIndex)=> {
        linkArray = dirArray[dirKey];
        if(filter.dir == 2) {  //Direction filter
          Object.keys(linkArray).forEach((linkKey, linkIndex)=> {
            if(filter.type != 'sub')) {
              dataObject = linkArray[linkKey];

              //ERROR with scoping occurs below.  Need to add parent scope.
              console.log(this.testString);
            }
          });
        }
      });
    }
}));

要么

在变量中定义this的根(在这种情况下为var that ):

var that = this;
let testString = "Parent Scope Accessed!";

Object.keys(data).forEach((function(Key, Index) => {
    if(filter.type == 'parameter') {
      Object.keys(dirArray).forEach(function(dirKey, dirIndex) => {
        linkArray = dirArray[dirKey];
        if(filter.dir == 2) {  //Direction filter
          Object.keys(linkArray).forEach(function(linkKey, linkIndex) => {
            if(filter.type != 'sub')) {
              dataObject = linkArray[linkKey];

              //ERROR with scoping occurs below.  Need to add parent scope.
              console.log(that.testString); //Use that instead of this here to refer to the parent scope
            }
          });
        }
      });
    }
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM