Цель проверки истинности объекта в IF l oop (если Object && ...) - PullRequest
1 голос
/ 04 мая 2020

Я пытался обернуть голову вокруг фрагмента кода в учебнике Node.Js (комментарии мои).

// All images array
  let allImages = [];    

// Returns array of filenames or objects at given path 'dirPath'
    let files = fs.readdirSync(dirPath);

// Loops through the array
for (file of files) {
      let fileLocation = path.join(dirPath, file);
// Retrieves information object about each file path
      var stat = fs.statSync(fileLocation);
// Recursively check each subdirectory for image files.
            if (stat && stat.isDirectory()) {
                getImagesFromDir(fileLocation);
// Checks if it is a file, and if the extension is either '.jpeg' or '.png'
// Why is the first condition (if stat) needed ??
            if (stat && stat.isFile() && ['.jpeg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
                allImages.push('images/'+file); 
      }
  }

Я понимаю, что fs.statSync (stat) выводит объект. Поэтому мне интересно:

Какова цель первого состояния?

if ( stat && ...
...