简体   繁体   English

无法在javascript / Polymer中调用我的递归函数

[英]Can not call my recursion function in javascript/Polymer

I have a function that searches through a tree of folders and finds the selected folder's parent folder. 我有一个功能,搜索文件夹树,找到所选文件夹的父文件夹。

Here is the function. 这是功能。

        getParentFolder: function (searchroot, childFolder) {
            searchroot.subfolders.forEach(function (folder) {
                if (folder.key == childFolder.key) {
                    return searchroot;
                }
                else {
                    if (folder.subfolders) {
                        return this.getParentFolder(folder, childFolder);
                    }
                }
            });
        }

When I call this with this.getParentFolder(rootFolder, childFolder); 当我用this.getParentFolder(rootFolder, childFolder);调用它时this.getParentFolder(rootFolder, childFolder);

It simply just gives me: Uncaught TypeError: this.getParentFolder is not a function Why is this? 它只是给了我:Uncaught TypeError:this.getParentFolder不是函数为什么会这样? In the same file I call other functions they work perfectly fine. 在同一个文件中,我称其他功能完全正常。 This is the only function that I fail to be able to call. 这是我无法调用的唯一功能。 Is it because of the recursion? 是因为递归吗?

You have to keep this in a variable as you change your context inside the forEach method. 你必须保持this ,你改变你的里面上下文变量forEach方法。

getParentFolder: function(searchroot, childFolder) {
  var self = this;
  searchroot.subfolders.forEach(function(folder) {
    if (folder.key == childFolder.key) {
      return searchroot;
    } else {
      if (folder.subfolders) {
        return self.getParentFolder(folder, childFolder);
      }
    }
  });
}

Also, return statement won't work the way you want. 此外, return语句将无法按您的方式工作。 I recommend you to enumerate the array using a for loop : 我建议你使用for循环枚举数组:

getParentFolder: function(searchroot, childFolder) {
  for (var i = 0; i < searchroot.subfolders.length; i++) {
    var folder = searchroot.subfolders[i];
    if (folder.key == childFolder.key) {
      return searchroot;
    } else {
      if (folder.subfolders) {
        return self.getParentFolder(folder, childFolder);
      }
    }
  }
}

The problem is that your this is different inside of function you passed into the forEach . 问题是,你this是函数内部不同的传递进forEach You need to bind the outer this to the inner function: 您需要将外部绑定this到内部功能:

getParentFolder: function(searchroot, childFolder) {
  searchroot.subfolders.forEach(function(folder) {
    if (folder.key == childFolder.key) {
      return searchroot;
    } else {
      if (folder.subfolders) {
        return this.getParentFolder(folder, childFolder);
      }
   }
  }, this); // pass in outer this as context for inner function
}

From Array.prototype.forEach() on MDN: 来自MDN上的Array.prototype.forEach()

Syntax: 句法:

 arr.forEach(function callback(currentValue, index, array) { //your iterator }[, thisArg]); 

Alternative Solution using ES6: 使用ES6的替代解决方案:

As mishu mentioned in the comments, the new ES6 arrow syntax also solves this problem. 正如mishu在评论中提到的,新的ES6箭头语法也解决了这个问题。 Your code in ES6 would look something like this: 您在ES6中的代码看起来像这样:

getParentFolder: function(searchroot, childFolder) {
  searchroot.subfolders.forEach((folder) => {
    if (folder.key == childFolder.key) {
      return searchroot;
    } else {
      if (folder.subfolders) {
        return this.getParentFolder(folder, childFolder);
      }
    }
  });
}

Arrow functions is ES6 do not bind this (see MDN ) so the outer this can be accessed from within the arrow function. 箭头功能是ES6不结合this (见MDN ),所以外this可以从箭头函数中进行访问。

Note that not all browsers support arrow functions yet (see Browser compatibility on MDN ). 请注意,并非所有浏览器都支持箭头功能(请参阅MDN上的浏览器兼容性 )。 To support older browsers, you can transpile ES6 to ES5 using Babel . 要支持旧版浏览器,您可以使用Babel将 ES6转换为ES5。

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

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