WXML
,可以构建出页面的结构。<!--wxml--><wxs module="m1">var msg = "hello world";module.exports.message = msg;</wxs><view>{{m1.message}}</view>
hello world
// page.jsPage({data: {array: [1, 2, 3, 4, 5, 1, 2, 3, 4],},})
<!--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>
5
<wxs>
标签内,或以.wxs
为后缀名的文件内。.wxs
文件和 <wxs>
标签都是一个单独的模块。module.exports
实现。.wxs
文件,在其中直接编写 WXS 脚本。// /pages/comm.wxsvar foo = "'hello world' from comm.wxs"var bar = function(d) {return d}module.exports = {foo: foo,bar: bar,}
/pages/comm.wxs
的文件里面编写了 WXS 代码。该 .wxs
文件可以被其他的 .wxs
文件或 WXML 中的 <wxs>
标签引用。wxs
模块均有一个内置的 module
对象。exports
:通过该属性,可以对外共享本模块的私有变量与函数。// /pages/tools.wxsvar foo = "'hello world' from tools.wxs"var bar = function(d) {return d}module.exports = {FOO: foo,bar: bar,}module.exports.msg = 'some msg'
<!-- page/index/index.wxml --><wxs src="./../tools.wxs" module="tools" /><view>{{tools.msg}}</view><view>{{tools.bar(tools.FOO)}}</view>
some msg'hello world' from tools.wxs
.wxs
模块中引用其他 wxs
文件模块,可以使用 require
函数。.wxs
文件模块,且必须使用相对路径。wxs
模块为单例模式,即在第一次引用时自动初始化为单例对象。不论在多个页面、多个地方、多次引用,都使用同一个 wxs
模块对象。wxs
模块在定义之后,一直没有被引用,则该模块不会被解析与运行。// /pages/tools.wxsvar foo = "'hello world' from tools.wxs"var bar = function(d) {return d}module.exports = {FOO: foo,bar: bar,}module.exports.msg = 'some msg'
// /pages/logic.wxsvar tools = require('./tools.wxs')console.log(tools.FOO)console.log(tools.bar('logic.wxs'))console.log(tools.msg)
<!-- /page/index/index.wxml --><wxs src="./../logic.wxs" module="logic" />
'hello world' from tools.wxslogic.wxssome msg
属性名 | 类型 | 默认值 |
module | string | 当前 <wxs> 标签的模块名。必填字段 |
src | string | 引用 .wxs 文件的相对路径。仅当本标签为单闭合标签或标签的内容为空时有效 |
<wxs>
标签的模块名。在单个 wxml 文件内,建议其值唯一。有重复模块名则按照先后顺序覆盖(后者覆盖前者)。不同文件之间的 WXS 模块名不会相互覆盖。<!--wxml--><wxs module="foo">var some_msg = "hello world";module.exports = { msg : some_msg, }</wxs><view>{{foo.msg}}</view>
hello world
wxs
文件模块。.wxs
文件模块,且必须使用相对路径。wxs
模块为单例模式,即在第一次引用时自动初始化为单例对象。不论在多个页面、多个地方、多次引用,都使用同一个 wxs
模块对象。wxs
模块在定义之后,一直没有被引用,则该模块不会被解析与运行。// /pages/index/index.jsPage({data: {msg: "'hello wrold' from js",},})
<!-- /pages/index/index.wxml --><wxs src="./../comm.wxs" module="some_comms"></wxs><!-- 也可以直接使用单标签闭合的写法 <wxs src="./../comm.wxs" module="some_comms" /> --><!-- 调用 some_comms 模块里面的 bar 函数,且参数为 some_comms 模块里面的 foo --><view>{{some_comms.bar(some_comms.foo)}}</view><!-- 调用 some_comms 模块里面的 bar 函数,且参数为 page/index/index.js 里面的 msg --><view>{{some_comms.bar(msg)}}</view>
'hello world' from comm.wxs'hello wrold' from js
/page/index/index.wxml
中通过 <wxs>
标签引用了 /page/comm.wxs
模块。undefined
。var foo = 1var bar = 'hello world'var i // i === undefined
foo
、 bar
、 i
三个变量。然后,foo
赋值为数值 1
,bar
赋值为字符串 "hello world"
。deletevoidtypeofnullundefinedNaNInfinityvarifelsetruefalserequirethisfunctionargumentsreturnforwhiledobreakcontinueswitchcasedefault
<!-- wxml --><wxs module="sample">// 方法一:单行注释/* 方法二:多行注释 *//* 方法三:结尾注释。即从 /* 开始往后的所有 WXS 代码均被注释 var a = 1; var b = 2; var c = "fake";</wxs>
*/
结束符。var a = 10, b = 20// 加法运算console.log(30 === a + b)// 减法运算console.log(-10 === a - b)// 乘法运算console.log(200 === a * b)// 除法运算console.log(0.5 === a / b)// 取余运算console.log(10 === a % b)
var a = '.q', b = 's'// 字符串拼接console.log('.wxs' === a + b)
var a = 10, b = 20// 自增运算console.log(10 === a++)console.log(12 === ++a)// 自减运算console.log(12 === a--)console.log(10 === --a)// 正值运算console.log(10 === +a)// 负值运算console.log(0 - 10 === -a)// 否运算console.log(-11 === ~a)// 取反运算console.log(false === !a)// delete 运算console.log(true === delete a.fake)// void 运算console.log(undefined === void a)// typeof 运算console.log('number' === typeof a)
var a = 10, b = 20// 左移运算console.log(80 === a << 3)// 无符号右移运算console.log(2 === a >> 2)// 带符号右移运算console.log(2 === a >>> 2)// 与运算console.log(2 === (a & 3))// 异或运算console.log(9 === (a ^ 3))// 或运算console.log(11 === (a | 3))
var a = 10, b = 20// 小于console.log(true === a < b)// 大于console.log(false === a > b)// 小于等于console.log(true === a <= b)// 大于等于console.log(false === a >= b)
var a = 10, b = 20// 等号console.log(false === (a == b))// 非等号console.log(true === (a != b))// 全等号console.log(false === (a === b))// 非全等号console.log(true === (a !== b))
var a = 10a = 10a *= 10console.log(100 === a)a = 10a /= 5console.log(2 === a)a = 10a %= 7console.log(3 === a)a = 10a += 5console.log(15 === a)a = 10a -= 11console.log(-1 === a)a = 10a <<= 10console.log(10240 === a)a = 10a >>= 2console.log(2 === a)a = 10a >>>= 2console.log(2 === a)a = 10a &= 3console.log(2 === a)a = 10a ^= 3console.log(9 === a)a = 10a |= 3console.log(11 === a)
var a = 10, b = 20// 逻辑与console.log(20 === (a && b))// 逻辑或console.log(10 === (a || b))
var a = 10, b = 20//条件运算符console.log(20 === (a >= 10 ? a + 10 : b + 10))//逗号运算符console.log(20 === (a, b))
优先级 | 运算符 | 说明 | 结合性 |
20 | ( ... ) | 括号 | n/a |
19 | ... . ... | 成员访问 | 从左到右 |
| ... [ ... ] | 成员访问 | 从左到右 |
| ... ( ... ) | 函数调用 | 从左到右 |
17 | ... ++ | 后置递增 | n/a |
| ... -- | 后置递减 | n/a |
16 | ! ... | 逻辑非 | 从右到左 |
| ~ ... | 按位非 | 从右到左 |
| + ... | 一元加法 | 从右到左 |
| - ... | 一元减法 | 从右到左 |
| ++ ... | 前置递增 | 从右到左 |
| -- ... | 前置递减 | 从右到左 |
| typeof ... | typeof | 从右到左 |
| void ... | void | 从右到左 |
| delete ... | delete | 从右到左 |
14 | ... * ... | 乘法 | 从左到右 |
| ... / ... | 除法 | 从左到右 |
| ... % ... | 取模 | 从左到右 |
13 | ... + ... | 加法 | 从左到右 |
| ... - ... | 减法 | 从左到右 |
12 | ... << ... | 按位左移 | 从左到右 |
| ... >> ... | 按位右移 | 从左到右 |
| ... >>> ... | 无符号右移 | 从左到右 |
11 | ... < ... | 小于 | 从左到右 |
| ... <= ... | 小于等于 | 从左到右 |
| ... > ... | 大于 | 从左到右 |
10 | ... == ... | 大于等于 | 从左到右 |
| ... != ... | 等号 | 从左到右 |
| ... === ... | 非等号 | 从左到右 |
| ... !== ... | 全等号 | 从左到右 |
9 | ... & ... | 按位与 | 从左到右 |
8 | ... ^ ... | 按位异或 | 从左到右 |
7 | ... | ... | 按位或 | 从左到右 |
6 | ... && ... | 逻辑或 | 从左到右 |
5 | ... || ... | 逻辑与 | 从左到右 |
4 | ... ? ... : ... | 条件运算符 | 从右到左 |
3 | ... = ... | 赋值 | 从右到左 |
| ... += ... | 赋值 | 从右到左 |
| ... -= ... | 赋值 | 从右到左 |
| ... *= ... | 赋值 | 从右到左 |
| ... /= ... | 赋值 | 从右到左 |
| ... %= ... | 赋值 | 从右到左 |
| ... <<= ... | 赋值 | 从右到左 |
| ... >>= ... | 赋值 | 从右到左 |
| ... >>>= ... | 赋值 | 从右到左 |
| ... &= ... | 赋值 | 从右到左 |
| ... ^= ... | 赋值 | 从右到左 |
| ... |= ... | 赋值 | 从右到左 |
0 | ... , ... | 逗号 | 从左到右 |
if (expression) statement
: 当 expression
为 truthy 时,执行 statement
。if (expression) statement1 else statement2:
当 expression
为 truthy 时,执行statement1
。 否则,执行 statement2
。if ... else if ... else statementN
通过该句型,可以在 statement1 ~ statementN
之间选其中一个执行。// if ...if (表达式) 语句if (表达式) 语句if (表达式) {代码块}// if ... elseif (表达式) 语句else 语句if (表达式) 语句else 语句if (表达式) {代码块} else {代码块}// if ... else if ... else ...if (表达式) {代码块} else if (表达式) {代码块} else if (表达式) {代码块} else {代码块}
switch (表达式) {case 变量:语句case 数字:语句breakcase 字符串:语句default:语句}
var exp = 10switch (exp) {case '10':console.log('string 10')breakcase 10:console.log('number 10')breakcase exp:console.log('var exp')breakdefault:console.log('default')}
number 10
for (语句; 语句; 语句) 语句for (语句; 语句; 语句) {代码块}
break
,continue
关键词。for (语句; 语句; 语句) 语句for (语句; 语句; 语句) {代码块}
01
while (表达式) 语句while (表达式) {代码块}do {代码块} while (表达式)
类型 | 说明 |
number | 数值 |
string | 字符串 |
boolean | 布尔值 |
object | 对象 |
function | 函数 |
array | 数组 |
date | 日期 |
regexp | 正则 |
var a = 10var PI = 3.141592653589793
constructor
:返回字符串 "Number"。'hello world'
true
和 false
。constructor
:返回字符串 "Boolean"
。var o = {} //生成一个新的空对象//生成一个新的非空对象o = {string: 1, //object 的 key 可以是字符串const_var: 2, //object 的 key 也可以是符合变量定义规则的标识符func: {}, //object 的 value 可以是任何类型}//对象属性的读操作console.log(1 === o['string'])console.log(2 === o.const_var)//对象属性的写操作o['string']++o['string'] += 10o.const_var++o.const_var += 10//对象属性的读操作console.log(12 === o['string'])console.log(13 === o.const_var)
constructor
:返回字符串 "Object"
console.log('Object' === { k: '1', v: '2' }.constructor)
toString
:返回字符串 "[object Object]"
//方法 1function a(x) {return x}//方法 2var b = function(x) {return x}
var a = function(x) {return function() {return x}}var b = a(100)console.log(100 === b())
arguments
关键词。该关键词目前只支持以下的属性:length
:传递给函数的参数个数。[index]
:通过 index
下标可以遍历传递给函数的每个参数。var a = function() {console.log(3 === arguments.length)console.log(100 === arguments[0])console.log(200 === arguments[1])console.log(300 === arguments[2])}a(100, 200, 300)
constructor
:返回字符串 "Function"
length
:返回函数的形参个数toString
:返回字符串 "[function Function]"
var func = function(a, b, c) {}console.log('Function' === func.constructor)console.log(3 === func.length)console.log('[function Function]' === func.toString())
var a = [] //生成一个新的空数组a = [1, '2', {}, function() {}] //生成一个新的非空数组,数组元素可以是任何类型
constructor
:返回字符串 "Array"
length
getDate()getDate(milliseconds)getDate(datestring)getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])
milliseconds
:从 1970 年 1 月 1 日 00:00:00 UTC 开始计算的毫秒数。datestring
:日期字符串,其格式为:"month day, year hours:minutes:seconds"。constructor
:返回字符串 “Date”。
regexp
对象需要使用 getRegExp 函数。getRegExp(pattern[, flags])
var a = getRegExp('x', 'img')console.log('x' === a.source)console.log(true === a.global)console.log(true === a.ignoreCase)console.log(true === a.multiline)
constructor
属性var number = 10console.log('Number' === number.constructor)var string = 'str'console.log('String' === string.constructor)var boolean = trueconsole.log('Boolean' === boolean.constructor)var object = {}console.log('Object' === object.constructor)var func = function() {}console.log('Function' === func.constructor)var array = []console.log('Array' === array.constructor)var date = getDate()console.log('Date' === date.constructor)var regexp = getRegExp()console.log('RegExp' === regexp.constructor)
typeof
也可以区分部分数据类型。var number = 10var boolean = truevar object = {}var func = function() {}var array = []var date = getDate()var regexp = getRegExp()console.log('number' === typeof number)console.log('boolean' === typeof boolean)console.log('object' === typeof object)console.log('function' === typeof func)console.log('object' === typeof array)console.log('object' === typeof date)console.log('object' === typeof regexp)console.log('undefined' === typeof undefined)console.log('object' === typeof null)
stringify(object)
:将 object
对象转换为 JSON
字符串,并返回该字符串。parse(string)
:将 JSON
字符串转化成对象,并返回该对象。console.log(undefined === JSON.stringify())console.log(undefined === JSON.stringify(undefined))console.log('null' === JSON.stringify(null))console.log('111' === JSON.stringify(111))console.log('"111"' === JSON.stringify('111'))console.log('true' === JSON.stringify(true))console.log(undefined === JSON.stringify(function() {}))console.log(undefined === JSON.parse(JSON.stringify()))console.log(undefined === JSON.parse(JSON.stringify(undefined)))console.log(null === JSON.parse(JSON.stringify(null)))console.log(111 === JSON.parse(JSON.stringify(111)))console.log('111' === JSON.parse(JSON.stringify('111')))console.log(true === JSON.parse(JSON.stringify(true)))console.log(undefined === JSON.parse(JSON.stringify(function() {})))
本页内容是否解决了您的问题?