# WXS

WXS (WeiXin Script) is a scripting language for Mini Programs. Combined with WXML, it can be used to construct the page structure.

# Note

  1. WXS does not depend on the runtime base library version and can run in all versions of Mini Programs.
  2. WXS is a different language from JavaScript. It has its own syntax, which is different from that of JavaScript.
  3. WXS runtime environment is isolated from other JavaScript code. WXS does not support calling functions defined in other JavaScript files and APIs provided by Mini Programs.
  4. WXS function cannot be called back as an event of a component.
  5. In Mini Programs on iOS, WXS is 2 to 20 times faster than JavaScript code, But on Android, they run almost equally fast.

The following are simple examples of WXS. For more information on WXS syntax, see WXS syntax.

# Page Rendering

<!--wxml-->
<wxs module="m1">
var msg = "hello world";

module.exports.message = msg;
</wxs>

<view> {{m1.message}} </view>

Page output:

hello world

# Data Processing

// page.js
Page({
  data: {
    array: [1, 2, 3, 4, 5, 1, 2, 3, 4]
  }
})
<!--wxml-->
<!-- The following getMax function takes an array and returns the value of the largest element in the array -->
<wxs module="m1">
var getMax = function(array) {
  var max = undefined;
  for (var i = 0; i < array.length; ++i) {
    max = max === undefined ?
      array[i] :
      (max >= array[i] ? max : array[i]);
  }
  return max;
}

module.exports.getMax = getMax;
</wxs>

<!-- Calls the getMax function in WXS with the "array" in page.js as the parameter -->
<view> {{m1.getMax(array)}} </view>

Page output:

5