Javascript set const variable inside of a try block

前端 未结 4 1670
故里飘歌
故里飘歌 2020-12-09 02:13

Is it possible in ES6 to set a variable inside of a try{} using const in strict mode?

\'use strict\';

const path          


        
4条回答
  •  無奈伤痛
    2020-12-09 02:20

    I would try to use a temp variable with let and assign that to a const var after the try/catch and 'delete' the temp var.

    'use strict';
    
    let temp;
    try {
      temp = path.resolve(process.cwd(), config);
    } catch (error) {
      //.....   
    }
    
    const configPath = temp;
    temp = undefined;
    
    console.log(configPath);
    

提交回复
热议问题