self 只读特性在 WorkerGlobalScope interface returns a reference to the WorkerGlobalScope itself. Most of the time it is a specific scope like DedicatedWorkerGlobalScope , SharedWorkerGlobalScope ,或 ServiceWorkerGlobalScope .

句法

var selfRef = self;
					

A global scope object (differs depending on the type of worker you are dealing with, as indicated above).

范例

If you called

console.log(self);
					

inside a worker, you will get a worker global scope of the same type as that worker object written to the console — something like the following:

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.
					

This provides a full list of the objects available to that worker scope, so it is quite a useful test if you want to see whether something is available to your worker or not. We also maintain a list of 可用于 Web 工作者的函数和类 .

规范

规范 状态 注释
HTML 实时标准
The definition of 'self' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
self Chrome 4 Edge 12 Firefox 3.5 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android 34 Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0

图例

完整支持

完整支持

另请参阅

WorkerGlobalScope

元数据

  • 最后修改: