Google Apps Script Redeclaration of Const Error

孤者浪人 提交于 2020-08-02 06:34:10

问题


Given this Google Apps Script script:

'use strict'
const foo = 2;
function bar() {
  Logger.log(foo + 2);
}

Running the function bar results in a TypeError: redeclaration of const foo.

Why? How is foo being redeclared?


回答1:


Seems like it's due to spotty implementation of ES6. I still get the error if I remove foo from the function, so the error is coming from the global const declaration. The below code produces the same error, but no error if you comment out const foo.

const foo = 2;

function bar() {
  const bar = 2;
  Logger.log(bar + 2);
}

See Google Apps Script Javascript Standard Support, in particular the first comment.




回答2:


From Apps Script const scoping problems

Avoid using const in Apps Script for now. It just doesn't work as it should. For now it's just a half baked version of var.

From the same source

Apps Script is based on ES3 JavaScript, with quite a few additions from ES5 and even ES6.

To read the primary source go to https://developers.google.com/apps-script/guides/services/#basic_javascript_features



来源:https://stackoverflow.com/questions/48293261/google-apps-script-redeclaration-of-const-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!