简体   繁体   English

foreach之前的Javascript分配(我不知道这叫什么)

[英]Javascript assignment before foreach (I don't know what it's called)

I'm going through some example code for looping through directories in Chrome App API and I've come across a statement I haven't seen before and don't know what to search. 我正在查看一些示例代码,这些示例代码用于遍历Chrome App API中的目录,并且遇到了一条我之前从未见过且不知道要搜索什么的语句。

function recurseDirectory(dirEntry, callback) {
    var dirsLeft = 1;
    var rootEntries;
    let helper = (dirEntry, isRoot) => {
        dirEntry.createReader().readEntries(entries => {
            dirsLeft--;

            if (isRoot) {
                rootEntries = entries;
            }
            //This line is what I'm confused about
            (dirEntry.entries = entries).forEach(entry => {
                if (entry.isDirectory) {
                    dirsLeft++;
                    helper(entry);
                }
            });

            if (!dirsLeft) {
                callback(rootEntries);
            }
        });
    }

    helper(dirEntry, true);
}

I can't run this code yet since it's just a snippet so I'm still dissecting it. 我还不能运行此代码,因为它只是一个代码段,因此我仍然对其进行剖析。

The part that I don't know is (dirEntry.entries = entries).forEach {...} . 我不知道的部分是(dirEntry.entries = entries).forEach {...} I tried looking up assignment foreach , equal foreach but no results for this. 我试图查找assignment foreachequal foreach但没有结果。

What is this operation/syntax called and what does it do? 此操作/语法称为什么,它起什么作用?

It's just an ordinary variable assignment - however, placing it in brackets means that you're assigning dirEntry.entries the value of entries , and iterating through entries with the same statement - it's just shorthand for: 这只是普通的变量分配-但是,将其放在方括号中意味着您正在分配dirEntry.entries entries的值, 使用相同的语句遍历entries -这只是以下各项的简写:

dirEntry.entries = entries;
entries.forEach(entry => {...});

This is not something else but an Assignment Expression. 这不是别的,而是赋值表达式。 (dirEntry.entries = entries) will return the value of entries on which forEach will be applied and in also dirEntry.entries to entires . (dirEntry.entries = entries)将返回将在其上应用forEachentries的值,并且还dirEntry.entries entires Its doing to things in one line. 一站式处理事情。

  • Changing the value of data.entires to entires data.entires的值更改为entires
  • return entires on which forEach is applied. 返回entiresforEach entires

它只是数组的赋值和forEach的立即执行,现在由entriesdirEntry.entries引用该数组。

This is just a lazy guy trying to save himself couple key press. 这只是一个懒惰的家伙,试图保存自己的几个按键。 It's equiv to 等价于

dirEntry.entries = entries;
entries.forEach(...)

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

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