尝试调用Object.assign()时,Google Apps脚本引发错误 [英] Google Apps Script throws error when trying to call Object.assign()

查看:108
本文介绍了尝试调用Object.assign()时,Google Apps脚本引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Apps脚本似乎无法识别功能Object.assign().尝试使用它时出现错误:

Google Apps Script doesn't appear to recognize the function Object.assign(). When trying to use it I get the error:

TypeError: Cannot find function assign in object function Object() { [native code for Object.Object, arity=1] }

我用来测试该功能的代码是直接从MDN复制的示例:

The code I'm testing the function with is the example copied directly from MDN:

function myFunction() {
  const object1 = {
    a: 1,
    b: 2,
    c: 3
  };

  const object2 = Object.assign({c: 4, d: 5}, object1); //error is thrown here

  console.log(object2.c, object2.d);
  // expected output: 3 5
}

我将上面的代码直接复制粘贴到Chrome开发者控制台中,该控制台运行良好并提供了预期的输出.

I copy-pasted the above code directly into the Chrome developer console, which ran fine and gave the expected output.

我在网上找不到任何内容说明应用程序脚本不支持此特定功能或类似功能.发生什么了?我该如何解决?

I can't find anything online saying that this particular function is not supported by apps script, or anything like that. So, what's going on? How can I fix this?

推荐答案

Apps脚本(在撰写本文时)是Ecmascript标准的完整实现,目前不本地支持Object.assign() .但是,您可以利用polyfill(可行时)来添加所需的功能.

Apps Script is NOT (as of this writing) a full implementation of the Ecmascript standard and currently does not natively support Object.assign(). However, you can leverage polyfills (when viable) that can add the needed functionality.

以下链接上的MDN上有一个Object.assign()可用的polyfill:

There is a polyfill available for Object.assign() on MDN at the following link:

https://developer. mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill

注意:有传言称,Apps Script将在不久的将来进行升级,以使用Chrome的V8引擎,从而支持EcmaScript 2017.

Note: Rumor has it that Apps Script will be upgraded (at some point in the near future) to use Chrome's V8 engine thereby supporting EcmaScript 2017.

这篇关于尝试调用Object.assign()时,Google Apps脚本引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆