Skip to content

JS support

Running restrictions

For security reasons, dynamic execution of JS code is not supported in mini-games, that is:

  • It does not support the use of eval to execute JS code.
  • It does not support the use of new Function to create functions.

Standard ECMAScript support

The JS execution environment of mini-games is different on different platforms, which leads to different support for ECMAScript standards on different platforms.

In order to smooth out these differences as much as possible, the mini-game basic library has a built-in core-js Polyfill. core-js can fill in the missing standard APIs in the platform environment.

It should be noted that the differences in platform support for ECMAScript syntax cannot be smoothed out. When you need to use some advanced syntax, such as async/await, you need to use the ES6 to ES5 function to support these syntaxes.

APIs that cannot be polyfilled

The following APIs cannot be used in some low-version clients. Please note that you should try to avoid using: Proxy object.

Differences from the standard

Promise timing differences

Due to the limitations of iOS JavaScriptCore, Promise in iOS 15 and below is a polyfill simulated by setTimeout. This means that the tasks triggered by Promise are normal tasks, not microtasks, which leads to differences in Promise timing in iOS15 and below from the standard.

There is no difference in iOS 16 and above.

js
var arr = [];

setTimeout(() => arr.push(6), 0);
arr.push(1);
const p = new Promise((resolve) => {
  arr.push(2);
  resolve();
});
arr.push(3);
p.then(() => arr.push(5));
arr.push(4);
setTimeout(() => arr.push(7), 0);

setTimeout(() => {
  // Expected output: [1, 2, 3, 4, 5, 6, 7]
  // On iOS 15 mini game environment, the output will be: [1, 2, 3, 4, 6, 5, 7]
  console.log(arr);
}, 1000);

For the difference between normal tasks and microtasks, you can see in-depth: Microtasks and JavaScript runtime environment.