javascript 代码判断它是否会在 Google 应用脚本环境中运行的惯用方法是什么?

问题描述

我正在使用 Google Appscript,在最终理解了 V8 运行时(相当于标准库)和 Web API 之间的区别之后,我想我为在 appscrpt 中运行 javascript 所涉及的内容开发了一个不错的心理模型。>

但是,当我在 Google Appscript 环境中运行时,我现在正在寻找一种方法来检测特征 - 我不清楚这样做的惯用方法是什么。

例如,我知道我可以检查我是否在这样的 Nodejs 环境中,as outlined in another SO answer around feature detection

(typeof process !== 'undefined') &&
(typeof process.versions.node !== 'undefined')

而且我知道 Google Appscript 提供了各种服务和全局对象。

因此,一种方法可能是检查 against a list of Services listed in the Google Appscript docs as available when running scripts,例如 LoggerMenuUi。但是我可以看到其中一些足够通用,足以与您可能在 javascript 中看到的常见变量名称发生冲突。

是否有一组“幸运”的对象需要检查,或者是否有一种惯用的方法来检查您是否在 AppScript 中运行?

在我尝试从上面的列表中检测某些内容之前,似乎值得询问一下,然后发现有更好的方法

解决方法

嗯……写完这篇并环顾四周后,我正在学习针对 Url Fetch Service google 公开的特征检测,因为它相对于 google 而言是特定的,并且与 fetch浏览器中提供的 API。

我对其他想法持开放态度 - 我仍在考虑 appscript。