简体   繁体   English

Const Apps错误的Google Apps脚本重新声明

[英]Google Apps Script Redeclaration of Const Error

Given this Google Apps Script script: 鉴于此Google Apps脚本脚本:

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

Running the function bar results in a TypeError: redeclaration of const foo. 运行功能bar导致TypeError: redeclaration of const foo.

Why? 为什么? How is foo being redeclared? 如何重新宣布foo

Seems like it's due to spotty implementation of ES6. 似乎是因为ES6的执行不力。 I still get the error if I remove foo from the function, so the error is coming from the global const declaration. 如果我从函数中删除foo,我仍然会收到错误,因此错误来自全局const声明。 The below code produces the same error, but no error if you comment out const foo. 下面的代码产生相同的错误,但如果你注释掉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. 请参阅Google Apps脚本Javascript标准支持 ,特别是第一条评论。

From Apps Script const scoping problems Apps脚本中查找范围问题

Avoid using const in Apps Script for now. 现在避免在Apps脚本中使用const。 It just doesn't work as it should. 它只是不能正常工作。 For now it's just a half baked version of var. 目前它只是var的半烘焙版本。

From the same source 来自同一个来源

Apps Script is based on ES3 JavaScript, with quite a few additions from ES5 and even ES6. Apps脚本基于ES3 JavaScript,ES5甚至ES6都有很多新增功能。

To read the primary source go to https://developers.google.com/apps-script/guides/services/#basic_javascript_features 要阅读主要来源,请访问https://developers.google.com/apps-script/guides/services/#basic_javascript_features

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM