我可以在 Google Apps Scripts 中使用 ES6 (V8) 库中定义的类吗? [英] Can I use in Google Apps Scripts a defined Class in a library with ES6 (V8)?

查看:18
本文介绍了我可以在 Google Apps Scripts 中使用 ES6 (V8) 库中定义的类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在库中定义的类,但结果我只收到一个错误.

[LibraryProject]/library/model/Update.gs

class 更新 {构造函数(obj = {}){if(typeof obj == "string"){选项 = JSON.parse(obj);}Object.assign(this, obj);}文本(){返回 (this.message && this.message.text)?this.message.text:''}}

任务

✅ 创建项目的新版本.(文件 > 管理版本...)

✅ 在另一个项目中加载这个库 [别名:CustomService] (资源 > 库...)

✅ 使用 CustomService 的功能

❌ 使用 CustomService 类

如果我尝试使用类

[NormalProject]/index.gs

函数测试(){Logger.log(CustomService.libraryFunction())var update = new CustomService.Update("");Logger.log(更新)}

<块引用>

TypeError: CustomService.Update is not a constructor (line 3, archivo "Code")

如何实例化此类的对象?

如果我跑...

记录器

解决方案

如官方文档所写,

只有脚本中的以下属性可供库用户使用:

<块引用>
  • 可枚举的全局属性
    • 函数声明,
    • 变量在函数外创建,使用 var 和
    • 在全局对象上显式设置的属性.

这意味着库用户可以使用全局 this 对象中的每个属性.

在 ES6 之前,函数外的所有声明(以及函数声明本身)都是这个全局对象的属性.ES6之后,有两种全局记录:

<块引用>
  • 对象记录 - 与 ES5 相同.

    • 函数声明
    • 函数生成器
    • 变量赋值
  • 声明性记录 - 新

    • 其他 - letconstclass

声明性记录中的那些不能从全局对象"访问,尽管它们本身是全局对象.因此,库用户无法访问库中的类声明.您可以简单地向类添加一个变量赋值,以将属性添加到全局对象(在任何函数之外):

var Update = class Update{/*你的代码在这里*/}

参考:

I'm trying to use a class defined in a library but I only receive an error as a result.

[LibraryProject]/library/model/Update.gs

class Update {
  constructor(obj = {}) {
    if(typeof obj == "string"){
      options = JSON.parse(obj);
    }
    Object.assign(this, obj);
  }

  text(){
    return (this.message && this.message.text)?this.message.text:''
  }
}

TASKS

✅ Create a new version of the project. (File > Manage versions...)

✅ Load this library in another project [Alias: CustomService] (Resources > Libraries...)

✅ Use functions of CustomService

❌ Use class of CustomService

If I try to use a Class

[NormalProject]/index.gs

function test  (){
  Logger.log(CustomService.libraryFunction())
  var update = new CustomService.Update("");
  Logger.log(update)
}

TypeError: CustomService.Update is not a constructor (línea 3, archivo "Code")

How can I instantiate an Object of this Class?

If I run...

Logger

解决方案

As written in the official documentation,

Only the following properties in the script are available to library users:

  • enumerable global properties
    • function declarations,
    • variables created outside a function with var, and
    • properties explicitly set on the global object.

This would mean every property in the global this object are available to library users.

Before ES6, All declarations outside a function (and function declaration themselves) were properties of this global object. After ES6, There are two kinds of global records:

  • Object record- Same as ES5.

    • Function declarations
    • Function generators
    • Variable assignments
  • Declarative record - New

    • Everything else - let, const, class

Those in the declarative record are not accessible from the global "object", though they are globals themselves. Thus, the class declaration in the library is not accessible to library users. You could simply add a variable assignment to the class to add a property to the global object(outside any function):

var Update = class Update{/*your code here*/}

References:

这篇关于我可以在 Google Apps Scripts 中使用 ES6 (V8) 库中定义的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆