A WebAssembly.Instance object is a stateful, executable instance of a WebAssembly.Module . Instance objects contain all the Exported WebAssembly functions that allow calling into WebAssembly code from JavaScript.

构造函数

WebAssembly.Instance()
创建新的 Instance 对象。

实例特性

Instance.prototype.exports

Returns an object containing as its members all the functions exported from the WebAssembly module instance, to allow them to be accessed and used by JavaScript. Read-only.

范例

Synchronously instantiating a WebAssembly module

WebAssembly.Instance() constructor function can be called to synchronously instantiate a given WebAssembly.Module object, for example:

const importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};
fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(bytes => {
  let mod = new WebAssembly.Module(bytes);
  let instance = new WebAssembly.Instance(mod, importObject);
  instance.exports.exported_func();
})
					

The preferred way to get an Instance is asynchronously, for example using the WebAssembly.instantiateStreaming() function like this:

const importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};
WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject)
.then(obj => obj.instance.exports.exported_func());
					

This also demonstrates how the exports property is used to access exported functions.

规范

规范
WebAssembly JavaScript 接口
The definition of 'Instance' 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 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Instance Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
Instance() 构造函数 Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
exports Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: