Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script throws error when trying to call Object.assign() [duplicate]

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?

like image 957
Joel Rummel Avatar asked Aug 16 '18 13:08

Joel Rummel


1 Answers

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.

like image 84
TheAddonDepot Avatar answered Sep 29 '22 08:09

TheAddonDepot