支持的运行时功能 - Amazon AppSync
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

支持的运行时功能

以下几节介绍了支持的 APPSYNC_JS 运行时功能集。

核心功能

支持以下核心功能。

类型

支持以下类型:

  • 数字

  • 字符串

  • 布尔值

  • objects

  • 数组

  • 函数

运算符

支持的运算符,其中包括:

  • 标准数学运算符(+-/%* 等)

  • Null 合并运算符 (??)

  • 可选链 (?.)

  • 按位运算符

  • voidtypeof 运算符

不支持以下运算符:

  • 一元运算符(++--~

  • in 运算符

    注意

    可以使用 Object.hasOwn 运算符检查指定的属性是否位于指定的对象中。

语句

支持以下语句:

  • const

  • let

  • var

  • break

  • else

  • for-in

  • for-of

  • if

  • return

  • switch

  • spread syntax

不支持以下语句:

  • catch

  • continue

  • do-while

  • finally

  • for(initialization; condition; afterthought)

    注意

    例外情况是 for-infor-of 表达式,支持这些表达式。

  • throw

  • try

  • while

  • 带标签的语句

文本

支持以下 ES 6 模板文本

  • 多行字符串

  • 表达式插值

  • 嵌套模板

函数

支持以下函数语法:

  • 支持函数声明。

  • 支持 ES 6 箭头函数。

  • 支持 ES 6 剩余参数语法。

严格模式

默认情况下,函数在严格模式下运行,因此您无需在函数代码中添加 use_strict 语句。无法对其进行更改。

原语对象

支持以下 ES 基元对象及其函数。

Object

支持以下对象:

  • Object.assign()

  • Object.entries()

  • Object.hasOwn()

  • Object.keys()

  • Object.values()

  • delete

字符串

支持以下字符串:

  • String.prototype.length()

  • String.prototype.charAt()

  • String.prototype.concat()

  • String.prototype.endsWith()

  • String.prototype.indexOf()

  • String.prototype.lastIndexOf()

  • String.raw()

  • String.prototype.replace()

    注意

    不支持正则表达式。

  • String.prototype.replaceAll()

    注意

    不支持正则表达式。

  • String.prototype.slice()

  • String.prototype.split()

  • String.prototype.startsWith()

  • String.prototype.toLowerCase()

  • String.prototype.toUpperCase()

  • String.prototype.trim()

  • String.prototype.trimEnd()

  • String.prototype.trimStart()

数字

支持以下数字:

  • Number.isFinite

  • Number.isNaN

内置对象和函数

支持以下函数和对象。

数学

支持以下数学函数:

  • Math.random()

  • Math.min()

  • Math.max()

  • Math.round()

  • Math.floor()

  • Math.ceil()

数组

支持以下数组方法:

  • Array.prototype.length

  • Array.prototype.concat()

  • Array.prototype.fill()

  • Array.prototype.flat()

  • Array.prototype.indexOf()

  • Array.prototype.join()

  • Array.prototype.lastIndexOf()

  • Array.prototype.pop()

  • Array.prototype.push()

  • Array.prototype.reverse()

  • Array.prototype.shift()

  • Array.prototype.slice()

  • Array.prototype.sort()

    注意

    Array.prototype.sort() 不支持参数。

  • Array.prototype.splice()

  • Array.prototype.unshift()

  • Array.prototype.forEach()

  • Array.prototype.map()

  • Array.prototype.flatMap()

  • Array.prototype.filter()

  • Array.prototype.reduce()

  • Array.prototype.reduceRight()

  • Array.prototype.find()

  • Array.prototype.some()

  • Array.prototype.every()

  • Array.prototype.findIndex()

  • Array.prototype.findLast()

  • Array.prototype.findLastIndex()

  • delete

控制台

可以使用控制台对象进行调试。在实时执行查询期间,控制台日志/错误语句将发送到 Amazon CloudWatch Logs(如果启用了日志记录)。在使用 evaluateCode 评估代码期间,将在命令响应中返回日志语句。

  • console.error()

  • console.log()

JSON

支持以下 JSON 方法:

  • JSON.parse()

    注意

    如果解析的字符串不是有效的 JSON,则返回空字符串。

  • JSON.stringify()

函数

  • 不支持 applybindcall 方法。

  • 不支持函数构造函数。

  • 不支持将函数作为参数传递。

  • 不支持递归函数调用。

Promise

不支持异步过程,也不支持 Promise。

注意

在 Amazon AppSync 上的 APPSYNC_JS 运行时环境中不支持网络和文件系统访问。Amazon AppSync 根据 Amazon AppSync 解析器或 Amazon AppSync 函数发出的请求处理所有 I/O 操作。

全局变量

支持以下全局约束:

错误类型

不支持使用 throw 引发错误。您可以使用 util.error() 函数返回错误。您可以使用 util.appendError 函数在 GraphQL 响应中包含错误。

有关更多信息,请参阅错误实用程序