From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../api/workerglobalscope/importscripts/index.html | 83 ++++++ files/zh-cn/web/api/workerglobalscope/index.html | 286 +++++++++++++++++++++ .../web/api/workerglobalscope/self/index.html | 81 ++++++ 3 files changed, 450 insertions(+) create mode 100644 files/zh-cn/web/api/workerglobalscope/importscripts/index.html create mode 100644 files/zh-cn/web/api/workerglobalscope/index.html create mode 100644 files/zh-cn/web/api/workerglobalscope/self/index.html (limited to 'files/zh-cn/web/api/workerglobalscope') diff --git a/files/zh-cn/web/api/workerglobalscope/importscripts/index.html b/files/zh-cn/web/api/workerglobalscope/importscripts/index.html new file mode 100644 index 0000000000..9a7781a1b8 --- /dev/null +++ b/files/zh-cn/web/api/workerglobalscope/importscripts/index.html @@ -0,0 +1,83 @@ +--- +title: WorkerGlobalScope.importScripts() +slug: Web/API/WorkerGlobalScope/importScripts +tags: + - API + - Web Workers + - WorkerGlobalScope + - Workers + - 参考 + - 方法 +translation_of: Web/API/WorkerGlobalScope/importScripts +--- +

{{APIRef("Web Workers API")}}

+ +

{{domxref("WorkerGlobalScope")}} 接口的importScripts() 方法将一个或多个脚本同步导入到工作者的作用域中。

+ +

语法

+ +
self.importScripts('foo.js');
+self.importScripts('foo.js', 'bar.js', ...);
+ +

参数

+ +

{{domxref("DOMString")}} 对象的一个逗号分隔列表,表示要导入的脚本。

+ +

返回值

+ +

无。

+ +

异常

+ + + + + + + + + + + + + + +
异常描述
NetworkError要导入的脚本不具有有效的 JavaScript MIME 类型(有效的类型如 text/javascript)。
+ +

例子

+ +

如果您在一个名为foo.js的单独脚本中编写了一些您想在worker.js中使用的功能,则可以使用以下行导入它:

+ +
importScripts('foo.js');
+ +

importScripts() 和 self.importScripts() 实际上是等效的 — 都表示从工作者的内部范围内调用的 importScripts()

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', '#dom-workerglobalscope-importscripts', 'importScripts()')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.WorkerGlobalScope.importScripts")}}

+ +

也可以看看

+ + diff --git a/files/zh-cn/web/api/workerglobalscope/index.html b/files/zh-cn/web/api/workerglobalscope/index.html new file mode 100644 index 0000000000..8d9fef8dd7 --- /dev/null +++ b/files/zh-cn/web/api/workerglobalscope/index.html @@ -0,0 +1,286 @@ +--- +title: WorkerGlobalScope +slug: Web/API/WorkerGlobalScope +tags: + - API + - Interface + - NeedsBrowserCompatibility + - NeedsTranslation + - Reference + - TopicStub + - WorkerGlobalScope + - Workers +translation_of: Web/API/WorkerGlobalScope +--- +

{{APIRef("Web Workers API")}}

+ +

 Web Workers API  的 WorkerGlobalScope 接口 是一个代表了任何 scope of worker的接口. Workers 没有浏览内容; 这个 scope 包含的信息总是通过 {{domxref("Window")}} objects传递 — 比如 event handlers, the console or the associated {{domxref("WorkerNavigator")}} object.每个 WorkerGlobalScope 都有自己的事件循环.

+ +

每个 worker type都有专门的这个接口: {{domxref("DedicatedWorkerGlobalScope")}} for dedicated workers, {{domxref("SharedWorkerGlobalScope")}} for shared workers, and {{domxref("ServiceWorkerGlobalScope")}} for ServiceWorkerself 属性返回每个内容的专门 scope .

+ +

Properties

+ +

This interface inherits properties from the {{domxref("EventTarget")}} interface and implements properties from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.

+ +

Standard properties

+ +
+
{{domxref("WorkerGlobalScope.caches")}} {{readOnlyinline}}
+
Returns the {{domxref("CacheStorage")}} object associated with the current context. This object enables service worker functionality such as storing assets for offline use, and generating custom responses to requests.
+
{{domxref("WorkerGlobalScope.navigator")}} {{readOnlyinline}}
+
Returns the {{domxref("WorkerNavigator")}} associated with the worker. It is a specific navigator object, mostly a subset of the {{domxref("Navigator")}} for browsing scopes, but adapted to workers.
+
{{domxref("WorkerGlobalScope.self")}} {{readOnlyinline}}
+
Returns a reference to the WorkerGlobalScope itself. Most of the time it is a specific scope like {{domxref("DedicatedWorkerGlobalScope")}},  {{domxref("SharedWorkerGlobalScope")}} or {{domxref("ServiceWorkerGlobalScope")}}.
+
{{domxref("WorkerGlobalScope.location")}} {{readOnlyinline}}
+
Returns the {{domxref("WorkerLocation")}} associated with the worker. It is a specific location object, mostly a subset of the {{domxref("Location")}} for browsing scopes, but adapted to workers.
+
+ +

Non-standard properties

+ +
+
{{domxref("WorkerGlobalScope.performance")}} {{readOnlyinline}}
+
Returns the {{domxref("Performance")}} associated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers.
+
{{domxref("WorkerGlobalScope.console")}} {{readOnlyinline}} {{Non-standard_inline}}
+
Returns the {{domxref("Console")}} associated with the worker.
+
+ +

Event Handlers

+ +

This interface inherits event handlers from the {{domxref("EventTarget")}} interface and implements event handlers from {{domxref("WindowTimers")}}, and {{domxref("WindowBase64")}}.

+ +
+
{{domxref("WorkerGlobalScope.onerror")}}
+
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("error")}} event is raised.
+
{{domxref("WorkerGlobalScope.onoffline")}}
+
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("offline")}} event is raised.
+
{{domxref("WorkerGlobalScope.ononline")}}
+
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("online")}} event is raised.
+
{{domxref("WorkerGlobalScope.onlanguagechange")}}
+
An {{domxref("EventHandler")}} fired at the global/worker scope object when the user's preferred languages change.
+
+ +
+
{{domxref("WorkerGlobalScope.onclose")}} {{non-standard_inline}}
+
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("close")}} event is raised.
+
{{domxref("WorkerGlobalScope.onrejectionhandled")}} {{non-standard_inline}}
+
An event handler for handled Promise rejection events.
+
{{domxref("WorkerGlobalScope.onunhandledrejection")}} {{non-standard_inline}}
+
An event handler for unhandled Promise rejection events.
+
+ +

Methods

+ +

This interface inherits methods from the {{domxref("EventTarget")}} interface and implements methods from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, {{domxref("WindowEventHandlers")}}, and {{domxref("GlobalFetch")}}.

+ +

Standard methods

+ +
+
{{domxref("WorkerGlobalScope.close()")}}
+
Discards any tasks queued in the WorkerGlobalScope's event loop, effectively closing this particular scope.
+
{{domxref("WorkerGlobalScope.importScripts()")}}
+
Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example: importScripts('foo.js', 'bar.js');
+
+ +

Non-standard methods

+ +
+
{{domxref("WorkerGlobalScope.dump()")}} {{non-standard_inline}}
+
Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's {{domxref("window.dump")}}, but for workers.
+
+ +

Methods implemented from elsewhere

+ +
+
{{domxref("WindowBase64.atob()")}}
+
Decodes a string of data which has been encoded using base-64 encoding.
+
{{domxref("WindowBase64.btoa()")}}
+
Creates a base-64 encoded ASCII string from a string of binary data.
+
{{domxref("WindowTimers.clearInterval()")}}
+
Cancels the repeated execution set using {{domxref("WindowTimers.setInterval()")}}.
+
{{domxref("WindowTimers.clearTimeout()")}}
+
Cancels the repeated execution set using {{domxref("WindowTimers.setTimeout()")}}.
+
{{domxref("ImageBitmapFactories.createImageBitmap()")}}
+
Accepts a variety of different image sources, and returns a {{domxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}}.
+
{{domxref("GlobalFetch.fetch()")}}
+
Starts the process of fetching a resource.
+
{{domxref("WindowTimers.setInterval()")}}
+
Schedules the execution of a function each X milliseconds.
+
{{domxref("WindowTimers.setTimeout()")}}
+
Sets a delay for executing a function.
+
+ +

Example

+ +

You won't access WorkerGlobalScope directly in your code; however, its properties and methods are inherited by more specific global scopes such as {{domxref("DedicatedWorkerGlobalScope")}} and {{domxref("SharedWorkerGlobalScope")}}. For example, you could import another script into the worker and print out the contents of the worker scope's navigator object using the following two lines:

+ +
importScripts('foo.js');
+console.log(navigator);
+ +
+

Note: Since the global scope of the worker script is effectively the global scope of the worker you are running ({{domxref("DedicatedWorkerGlobalScope")}} or whatever) and all worker global scopes inherit methods, properties, etc. from WorkerGlobalScope, you can run lines such as those above without specifying a parent object.

+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#workerglobalscope', 'WorkerGlobalScope')}}{{Spec2('HTML WHATWG')}}No change from {{SpecName("Web Workers")}}.
{{SpecName('Service Workers')}}{{Spec2('Service Workers')}}Defines caches.
{{SpecName('Web Workers', '#workerglobalscope', 'WorkerGlobalScope')}}{{Spec2('Web Workers')}}Initial definition
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(4)}}{{CompatGeckoDesktop("1.9.1")}}1010.64
ononline, onoffline{{CompatVersionUnknown}}{{CompatGeckoDesktop("29")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
console {{Non-standard_inline}}{{CompatVersionUnknown}}{{CompatGeckoDesktop("29")}}[1]
+ {{CompatGeckoDesktop("30")}}
{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
performance{{CompatVersionUnknown}}{{CompatGeckoDesktop("34")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
caches{{CompatChrome(40)}}{{CompatUnknown}}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)Firefox OS (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("1.9.1")}}1.0.11011.55.1
ononline, onoffline{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("29")}}[1]
+ {{CompatGeckoMobile("30")}}
1.4{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
console {{Non-standard_inline}}{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("29")}}1.4{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
performance{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("34")}}2.1{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
caches{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

[1] Gecko 29 implemented this as WorkerConsole. Since version 30 it uses the regular Console.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/workerglobalscope/self/index.html b/files/zh-cn/web/api/workerglobalscope/self/index.html new file mode 100644 index 0000000000..fbabf105e3 --- /dev/null +++ b/files/zh-cn/web/api/workerglobalscope/self/index.html @@ -0,0 +1,81 @@ +--- +title: WorkerGlobalScope.self +slug: Web/API/WorkerGlobalScope/self +translation_of: Web/API/WorkerGlobalScope/self +--- +
{{APIRef("Web Workers API")}}
+ +

self 是 {{domxref("WorkerGlobalScope")}} 的只读属性,它指向 WorkerGlobalScope 自身。通常情况下,它是如 {{domxref("DedicatedWorkerGlobalScope")}},  {{domxref("SharedWorkerGlobalScope")}}, 或 {{domxref("ServiceWorkerGlobalScope")}} 类型的 scope 。

+ +

语法

+ +
var selfRef = self;
+ +

+ +

当前 worker 的全局 scope (值取决于你创建的 worker 类型)。

+ +

示例

+ +

在 worker 中运行这行代码:

+ +
console.log(self);
+ +

console 中会输出当前 worker 的全局 scope,如下所示:

+ +
DedicatedWorkerGlobalScope {
+undefined: undefined, Infinity: Infinity, Math: MathConstructor, NaN: NaN, Intl: Object…}
+    Infinity: Infinity
+    Array: function Array() { [native code] }
+      arguments: null
+      caller: null
+      isArray: function isArray() { [native code] }
+      length: 1
+      name: "Array"
+      observe: function observe() { [native code] }
+      prototype: Array[0]
+      unobserve: function unobserve() { [native code] }
+      __proto__: function Empty() {}
+      <function scope>
+    ArrayBuffer: function ArrayBuffer() { [native code] }
+    Blob: function Blob() { [native code] }
+    Boolean: function Boolean() { [native code] }
+    DataView: function DataView() { [native code] }
+    Date: function Date() { [native code] }
+    DedicatedWorkerGlobalScope: function DedicatedWorkerGlobalScope() { [native code] }
+    Error: function Error() { [native code] }
+// etc. etc.
+
+ +

这会列出当前 worker scope 上完整的属性列表,在需要检测某个属性是否可用时非常有用。 详细列表也可查阅 Functions and classes available to Web Workers.

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-workerglobalscope-self', 'self')}}{{Spec2('HTML WHATWG')}} 
+ +

兼容性

+ +

 

+ + + +

{{Compat("api.WorkerGlobalScope.self")}}

+ +

 

+ +

参见

+ +

{{domxref("WorkerGlobalScope")}}

-- cgit v1.2.3-54-g00ecf