> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/contributor/contributor-zh/chang-jing-yun-xing-shi/globals.md).

# 全局变量

场景运行时除了标准工具（例如 `Promise`, `Date` 或 `Math`.

{% hint style="info" %}
下文所述的全局对象是典型的 JavaScript 概念，但它们针对 Decentraland 场景运行时进行了定制，行为可能与浏览器或 Node 环境中的对应对象并不完全相同。
{% endhint %}

### 全局变量

运行时会在场景的全局作用域中注入 6 个定义：

1. [`console`](#console)：标准 `console` 对象。
2. [`exports`](#module)：场景可以添加其 [公共接口](https://github.com/decentraland/docs/blob/main/contributor/execution.md).
3. [`模块`](#module)：一个容器，用于 `exports` 对象。
4. [`require`](#module)：一个按名称加载运行时提供模块的函数。
5. [`fetch`](#http)：浏览器 `fetch` 函数的受限实现。
6. [`WebSocket`](#http)：浏览器 `WebSocket` 类。

以上所有内容都定义为只读属性，因此不能重新赋值。其中一些在使用时会抛出异常，除非向场景授予某些 [权限](https://github.com/decentraland/docs/blob/main/contributor/contributor/content/entity-types/scenes.md#permissions) 权限。

### Console <a href="#console" id="console"></a>

场景可以访问一个 `console` 对象，类似于浏览器或 Node 环境提供的对象，不过仅限于你通常能找到的部分方法。

```ts
type Console = {
  log(...args: any): void
  info(...args: any): void
  debug(...args: any): void
  warning(...args: any): void
  error(...args: any): void
}
```

和标准对应方法一样，每个方法都接受任意类型的可变参数，并将它们渲染为人类可读的消息。例如，下面是有效的：

```js
console.log("那个东西刚刚出现了", { thing: "foo" }, [1, 2, 3])
```

这些方法的确切行为取决于提供方，但这些消息必须能供调试代码的场景开发者访问。

{% hint style="info" %}
在 Foundation 基于浏览器的 World Explorer 中，日志消息会显示在开发者工具面板中。
{% endhint %}

### 导入与导出 <a href="#module" id="module"></a>

场景可以使用传统的 [CommonJS](https://wiki.commonjs.org/wiki/Modules/1.0) 模块接口来导入和导出对象。

```ts
exports: Object
require(moduleName: string): Object
```

该 `require` 函数允许场景访问运行时提供的模块（例如 [EngineApi](https://github.com/decentraland/docs/blob/main/contributor/modules/engine_api.md) 或 [RestrictedActions](https://github.com/decentraland/docs/blob/main/contributor/modules/restricted_actions.md)），除此之外别无其他（它 **不会** 通过路径访问 NPM 包或模块）。

添加到 `exports` 对象中的属性就是场景的公共接口，并会暴露给运行时。实际上，场景 *必须* 至少暴露一个方法才能正常运行（参见 [执行](https://github.com/decentraland/docs/blob/main/contributor/execution.md)).

{% hint style="info" %}
用 TypeScript 等语言编写的场景使用更现代的 `import` 和 `export` 语句，这些语句可以被转译为与 CommonJS 兼容的 `require` 和 `exports`.
{% endhint %}

### HTTP 和 WebSocket <a href="#http" id="http"></a>

该 `fetch` 和 `WebSocket` 全局变量的工作方式与其广为人知的对应对象完全相同（参见 [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) 和 [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) MDN 中的说明），但运行时会强制施加一些限制。

在调用 `fetch` 函数时：

* 如果 URL 不以 `https://`.
* 开头，则会抛出错误。 [`USE_FETCH` 权限](https://github.com/decentraland/docs/blob/main/contributor/contributor/content/entity-types/scenes.md#permissions).
* 实现定义的超时可能会中止请求。

在使用 `WebSocket` 类时：

* 如果 URL 不以 `wss:`
* 开头，则会抛出错误。 [`USE_WEBSOCKET` 权限](https://github.com/decentraland/docs/blob/main/contributor/contributor/content/entity-types/scenes.md#permissions).

除这些差异外，两种情况都遵循标准行为。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.decentraland.org/contributor/contributor-zh/chang-jing-yun-xing-shi/globals.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
