'Apps Script: why does name of properties cause "Maximum call stack size exceeded"?
- I'm using Apps Script to customize my Google Sheets;
- I have two different objects (one is private, the other one is public) with same properties;
- They interact with each other;
- Here follows an example:
function run(argument) {
let Public = {}
let Private = myClass().setFoo(argument)
Public.getFoo = Private.getFoo
return Public;
}
function myClass() {
let Public = {};
let Private = {};
Private.foo = null;
Public.setFoo = function (arg) { Private.foo = arg; return Public};
Public.getFoo = function () { return Private.foo };
return Public;
}
- I don't want setter to be available to the coder. That's why I'm doing this pattern;
- The problem is: when I run it (through other function somewhere), the log displays: "Maximum call stack size exceeded", pointing to the
Public.getFoo = Private.getFoo; - I've tried many ways to solve this, but the only thing that worked was changing the properties names, like
Public.getFoo = Private._getFoo
It seams like Apps Script IDE understand both of them are the same object, so it would call itself infinetely. But why does that happen if I'm not referring to the same object?
Solution 1:[1]
That's a behavior that comes from JavaScript ES2015 syntax (not a Apps Script feature per se). That behavior might be different in other languages (like Java) where the object can be detected as different.
In JavaScript, there's no a native way to create private variables, but that's why we use the workaround of putting an underscore before the variable name as a convention.
There's a great article about that by Basti Ortiz that mentions how JavaScript emulates private variables
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Salomon |
