why loadash concat instanceof Array is false in google apps script included library

Viewed 78

I'm using lodashgs https://github.com/contributorpw/lodashgs

For some reason, simple statement

_.concat([]) instanceof Array

is False, while

_.isArray([])

is True

In ordinary lodash, it's the same

console.log(_.concat([]) instanceof Array === _.isArray(_.concat([])))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

Does anyone know what's the magic happens in GAS for this issue?

1 Answers

When I tested your script, I confirmed the same situation with you. But I confirmed the following 2 patterns.

  • When the script of LodashGS is used as the library, the same situation with your upper situation can be confirmed.
    • console.log(_.concat([]) instanceof Array === _.isArray(_.concat([]))) returns false.
  • When the script of LodashGS is used in the same Google Apps Script project by copying the script from the repository, I confirmed that the result is the same with your bottom script ("Show code snippet").
    • console.log(_.concat([]) instanceof Array === _.isArray(_.concat([]))) returns true.

From above results, about the instance, the array retrieved from _ might be different from Array in the client side. I thought that this might be the reason of this issue. But this is my guess. As the test case, I experimented the following sample situation for LodashGS which is used as the library.

Test:

  1. Create new Google Apps Script project.

    • This is used as the library.
  2. Copy and paste the script of LodashGS.

    • And, please put the following script to the top of the script. By this, Array can be used from the library.

        var ar = Array;
      
  3. Deoloy the Google Apps Script project as the library.

  4. Create new another Google Apps Script project.

    • This is used as the client.
  5. Install the library as LodashGS.

  6. Copy and paste the following script for testing.

     function myFunction() {
       const _ = LodashGS.load();
       console.log(_.concat([]) instanceof Array === _.isArray(_.concat([])));
       console.log(_.concat([]) instanceof LodashGS.ar === _.isArray(_.concat([])));
     }
    
  7. Run the function of myFunction.

Result:

By above flow, console.log(_.concat([]) instanceof Array === _.isArray(_.concat([]))) and console.log(_.concat([]) instanceof LodashGS.ar === _.isArray(_.concat([]))) show false and true, respectively.

Note:

  • As an additional information, when V8 runtime is not used, console.log(_.concat([]) instanceof Array === _.isArray(_.concat([]))) and console.log(_.concat([]) instanceof LodashGS.ar === _.isArray(_.concat([]))) return true and true, respectively. From this situation, it is considered that one of the reasons of above issue is due to V8 runtime.
    • When you use V8 runtime, above workaround can be used.

Reference:

Related