# WXS
WXS (WeiXin Script) is a scripting language for Mini Programs. Combined with WXML
, it can be used to construct the page structure.
# Note
- WXS does not depend on the runtime base library version and can run in all versions of Mini Programs.
- WXS is a different language from JavaScript. It has its own syntax, which is different from that of JavaScript.
- 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.
- WXS function cannot be called back as an event of a component.
- 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