no-window-prefix
注意:此规则是
recommended
规则集的一部分。在
deno.json
中启用完整规则集{ "lint": { "tags": ["recommended"] } }
使用 Deno CLI 启用完整规则集
deno lint --tags=recommended
禁止通过 window
对象使用 Web API。
在大多数情况下,全局变量 window
的作用类似于 globalThis
。例如,您可以像 window.fetch(..)
而不是 fetch(..)` 或
globalThis.fetch(..)` 这样调用 `fetch` API。但是,在 Web Workers 中,`window` 不可用,而是 `self`、`globalThis` 或无前缀可以正常工作。因此,为了 Web Workers 和其他上下文之间的兼容性,强烈建议不要通过 `window` 访问全局属性。
某些 API,包括 window.alert
、window.location
和 window.history
,允许使用 window
调用,因为这些 API 在 Workers 中不受支持或具有不同的含义。换句话说,仅当 window
完全可以被 self
、globalThis
或无前缀替换时,此 lint 规则才会抱怨 window
的使用。
无效
const a = await window.fetch("https://deno.land");
const b = window.Deno.metrics();
有效
const a1 = await fetch("https://deno.land");
const a2 = await globalThis.fetch("https://deno.land");
const a3 = await self.fetch("https://deno.land");
const b1 = Deno.metrics();
const b2 = globalThis.Deno.metrics();
const b3 = self.Deno.metrics();
// `alert` is allowed to call with `window` because it's not supported in Workers
window.alert("🍣");
// `location` is also allowed
window.location.host;