Skip to content

Instantly share code, notes, and snippets.

@zhangkn
Created May 17, 2018 07:49
Show Gist options
  • Save zhangkn/f8d955588bb92d5ac489dc7fbd59d3c9 to your computer and use it in GitHub Desktop.
Save zhangkn/f8d955588bb92d5ac489dc7fbd59d3c9 to your computer and use it in GitHub Desktop.
<!--wxml-->
<!-- 下面的 getMax 函数,接受一个数组,且返回数组中最大的元素的值 -->
<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>
<!-- 调用 wxs 里面的 getMax 函数,参数为 page.js 里面的 array -->
<view> {{m1.getMax(array)}} </view>
// page.js
Page({
data: {
array: [1, 2, 3, 4, 5, 1, 2, 3, 4]
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment