5

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] }

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
}

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?

Joel Rummel
  • 802
  • 5
  • 18
  • Related: https://stackoverflow.com/questions/12279357/is-there-a-complete-definition-of-the-google-app-script-syntax-somewhere , https://stackoverflow.com/questions/37768501/google-apps-script-javascript-standard-support , and https://stackoverflow.com/questions/17252409/which-edition-of-ecma-262-does-google-apps-script-support – tehhowch Aug 16 '18 at 14:06

1 Answers1

7

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.

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

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

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.

TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30