private functons in app-script

2,216 views
Skip to first unread message

Paul Armstrong

unread,
Jul 23, 2022, 11:00:51 PM7/23/22
to Google Apps Script Community
Hi

I can not find anything that says that private functions are not supported by app-script, but it appears they are not. As per this example:

class Test {
  static test() {
    return Test.#test2();
  }
  static #test2() {
    return "tested";
  }
}
Test.test();

This code will not save. I get an Unexpected token ILLEGAL error, at the first line it encounters a # on. 

Thanks!

ShiizophreN

unread,
Jul 23, 2022, 11:52:31 PM7/23/22
to Google Apps Script Community
It works. When referencing the class through the Apps Script library, it has to be var x = new LibraryName.ClassName();

Paul Armstrong

unread,
Jul 24, 2022, 12:06:12 AM7/24/22
to Google Apps Script Community
Hi 
Thanks for your answer. 
I am not particularly familiar with Javascript. Can you please expand on your answer a little?
Do I still use # to identify private functions? It take it not, but then how? 

Thanks!

Andrew Roberts

unread,
Jul 24, 2022, 2:48:21 AM7/24/22
to google-apps-sc...@googlegroups.com
If you define the following object you get a private function.

const obj1 = (function(ns) {
  ns.publicFunction = function() {
    privateFunction()
  }
  return ns
  function privateFunction() {
    // Do something private
  }
})({})

obj1.publicFunction() // OK
obj1.privateFunction() // Error

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1afac8cf-7ceb-43aa-91fc-4bb555c79eb1n%40googlegroups.com.

Sohail

unread,
Jul 24, 2022, 9:56:53 AM7/24/22
to Google Apps Script Community
To my understanding of your question, since you are saying that the IDE is not saving the code but showing an error on the #, I would say please use an underscore before the function name to mark it as private in google apps script code.

dimud...@gmail.com

unread,
Jul 24, 2022, 10:26:39 AM7/24/22
to Google Apps Script Community
It is a known issue that Google plans to address at some point.

See bug report on Google's issue tracker:
https://issuetracker.google.com/issues/239767510

In any case, classes in Javascript are just syntactic sugar, you're still using prototype chains under the hood. If you really need private class members you can leverage closures (as Andrew Roberts already suggested) and/or a WeakMap.

Paul Armstrong

unread,
Jul 24, 2022, 4:46:38 PM7/24/22
to Google Apps Script Community
Hi
Thank you all for your answers. 
Big thank you to dimud...@gmail.com - who nailed my question. 
I think I will go with "_" prefixes for the time being and wait until it's fixed. My intention is mostly about writing clean, maintainable, code.

dimud...@gmail.com

unread,
Jul 24, 2022, 7:21:41 PM7/24/22
to Google Apps Script Community
OOP isn't the only paradigm that allows you to write clean and maintainable code. You might want to try your hand at functional programming.

The following guide should help:

https://mostly-adequate.gitbook.io/mostly-adequate-guide/

Paul Armstrong

unread,
Jul 24, 2022, 7:23:16 PM7/24/22
to Google Apps Script Community
Thanks!
Reply all
Reply to author
Forward
0 new messages