# WXS
WXS(WeiXin Script) is inline in the WXML The script segment in. adopt WXS A small number of processing scripts can be inlined in the template to enrich the template's data preprocessing capabilities. Also, WXS Can also be used to write simple WXS Event response function。
Grammatically, WXS Similar to those with a small number of restrictions on JavaScript To fully understand the WXS Syntax, please refer to theWXS Syntax Reference。
The following are some examples of using WXS A simple example of.
# 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 that 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>
<!-- call wxs Inside. getMax Function, the argument is page.js Inside. array -->
<view> {{m1.getMax(array)}} </view>
Page Output:
5