最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Apps Script Redeclaration of Const Error - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Jan 17, 2018 at 3:57 Jules Bertholet asked Jan 17, 2018 at 3:44 Jules BertholetJules Bertholet 3133 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

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 ing from the global const declaration. The below code produces the same error, but no error if you ment 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 ment.

UPDATE: Using the new runtime (V8) the code in the questions doesn't throw the error anymore, but disabling it, the old runtime (Rhino) still throws the error.

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.

The above was referring to using the runtime available at the time that it was published, before the new runtime was available.

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./apps-script/guides/services/#basic_javascript_features

发布评论

评论列表(0)

  1. 暂无评论