javascript - 应用程序已在 try/catch 中声明

标签 javascript electron eslint

我有以下try/catch:

try {
    var { app } = require('electron').remote;
} catch (e) {
    var { app } = require('electron');
}

尽管它是有效的,但 ESLint 存在一个问题,即 app 已被声明。

因此,我尝试将 var 移至 try/catch 上方,但 = 出现错误,表明它是一个意外 token 像这样:

var app;
try {
    { app } = require('electron').remote;
} catch (e) {
    { app } = require('electron');
}

执行此操作的正确方法是什么,以便 ESLint 不会提示?

最佳答案

首先,由于您显然使用的是 ES2015+(又名“ES6+”),请考虑使用 letconst 而不是 var 。在 ES2015+ 世界中, var 基本上没有用处。 (但以下两者也适用于 var。)

解析问题是因为 { 看起来像 block 的开头。您可以将整个作业 package 在 () 中来解决这个问题:

let app;
try {
    ({ app } = require('electron').remote);
} catch (e) {
    ({ app } = require('electron'));
}

示例(伪造 require):

// fake require
function require(name) {
  return {app: {name: "I'm the app"}};
}

let app;
try {
  ({ app } = require('electon').remote);
} catch (e) {
  ({ app } = require('electron'));
}
console.log(app);

或者根本不使用解构:

let app;
try {
    app = require('electron').remote.app;
} catch (e) {
    app = require('electron').app;
}

// fake require
function require(name) {
  return {app: {name: "I'm the app"}};
}

let app;
try {
  app = require('electron').remote.app;
} catch (e) {
  app = require('electron').app;
}
console.log(app);

关于javascript - 应用程序已在 try/catch 中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41987045/

相关文章:

javascript - 鼠标滚轮滚动DIV元素

electron - 如何从网站启动我的 Electron 应用程序

google-chrome-extension - 我们可以使用现有的代码库轻松地为 Electron 应用创建chrome扩展吗

javascript - 如何在箭头函数 HOC 中设置 Component displayName

typescript - eslint 提示 typescript 的路径别名

typescript - 我可以关闭用于 lambda 解构的 eslint tyfedef 规则吗

javascript - 在联系表单上提交时不要输入 "Leave this page"

javascript - 类型错误 : Cannot read property 'apply' of undefined

javascript - 两个嵌套线程可以在没有 $q.defer() 的情况下 fork 并加入 AngularJS $q 中吗?

Angular(5)-Electron 串口支持