?WXS
WXS(WeiXin Script)是小程序的一套腳本語(yǔ)言,結(jié)合WXML底洗,可以構(gòu)建出頁(yè)面的結(jié)構(gòu)。
以下是一些使用 WXS 的簡(jiǎn)單示例:
頁(yè)面渲染
頁(yè)面輸出:
hello world
數(shù)據(jù)處理
數(shù)據(jù)類型
WXS 語(yǔ)言目前共有以下幾種數(shù)據(jù)類型:
number: 數(shù)值
string:字符串
boolean:布爾值
object:對(duì)象
function:函數(shù)
array: 數(shù)組
date:日期
regexp:正則
number
語(yǔ)法
number 包括兩種數(shù)值:整數(shù)掷贾,小數(shù)隙姿。
var a =10;
var PI =3.141592653589793;
屬性
constructor:返回字符串"Number"。
方法
toString
toLocaleString
valueOf
toFixed
toExponential
toPrecision
以上方法的具體使用請(qǐng)參考ES5標(biāo)準(zhǔn)唉擂。
string
語(yǔ)法
string 有兩種寫法:
'hello world';"hello world";
屬性
constructor:返回字符串"String"餐屎。
length
除constructor外屬性的具體含義請(qǐng)參考ES5標(biāo)準(zhǔn)。
方法
toString
valueOf
charAt
charCodeAt
concat
indexOf
lastIndexOf
localeCompare
match
replace
search
slice
split
substring
toLowerCase
toLocaleLowerCase
toUpperCase
toLocaleUpperCase
trim
以上方法的具體使用請(qǐng)參考ES5標(biāo)準(zhǔn)玩祟。
boolean
語(yǔ)法
布爾值只有兩個(gè)特定的值:true 和 false腹缩。
屬性
constructor:返回字符串"Boolean"。
方法
toString
valueOf
以上方法的具體使用請(qǐng)參考ES5標(biāo)準(zhǔn)空扎。
object
語(yǔ)法
object 是一種無(wú)序的鍵值對(duì)藏鹊。使用方法如下所示:
varo = {}//生成一個(gè)新的空對(duì)象//生成一個(gè)新的非空對(duì)象o = {'string':1,//object 的 key 可以是字符串const_var :2,//object 的 key 也可以是符合變量定義規(guī)則的標(biāo)識(shí)符func? ? ? : {},//object 的 value 可以是任何類型};//對(duì)象屬性的讀操作console.log(1=== o['string']);console.log(2=== o.const_var);//對(duì)象屬性的寫操作o['string']++;o['string'] +=10;o.const_var++;o.const_var +=10;//對(duì)象屬性的讀操作console.log(12=== o['string']);console.log(13=== o.const_var);
屬性
constructor:返回字符串"Object"。
console.log("Object"=== {k:"1",v:"2"}.constructor)
方法
toString:返回字符串"[object Object]"转锈。
function
語(yǔ)法
function 支持以下的定義方式:
//方法 1functiona(x){returnx;}//方法 2varb =function(x){returnx;}
function 同時(shí)也支持以下的語(yǔ)法(匿名函數(shù)盘寡,閉包等):
vara =function(x){returnfunction(){returnx;}}varb = a(100);console.log(100=== b() );
arguments
function 里面可以使用arguments關(guān)鍵詞。該關(guān)鍵詞目前只支持以下的屬性:
length: 傳遞給函數(shù)的參數(shù)個(gè)數(shù)撮慨。
[index]: 通過(guò)index下標(biāo)可以遍歷傳遞給函數(shù)的每個(gè)參數(shù)竿痰。
示例代碼:
vara =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:返回函數(shù)的形參個(gè)數(shù)。
方法
toString:返回字符串"[function Function]"影涉。
示例代碼:
varfunc =function(a,b,c){ }console.log("Function"=== func.constructor);console.log(3=== func.length);console.log("[function Function]"=== func.toString());
array
語(yǔ)法
array 支持以下的定義方式:
vara = [];//生成一個(gè)新的空數(shù)組a = [1,"2",{},function(){}];//生成一個(gè)新的非空數(shù)組变隔,數(shù)組元素可以是任何類型
屬性
constructor:返回字符串"Array"。
length
除constructor外屬性的具體含義請(qǐng)參考ES5標(biāo)準(zhǔn)蟹倾。
方法
toString
concat
join
pop
push
reverse
shift
slice
sort
splice
unshift
indexOf
lastIndexOf
every
some
forEach
map
filter
reduce
reduceRight
以上方法的具體使用請(qǐng)參考ES5標(biāo)準(zhǔn)匣缘。
date
語(yǔ)法
生成 date 對(duì)象需要使用getDate函數(shù), 返回一個(gè)當(dāng)前時(shí)間的對(duì)象。
getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])
參數(shù)
milliseconds: 從1970年1月1日00:00:00 UTC開始計(jì)算的毫秒數(shù)
datestring: 日期字符串喊式,其格式為:"month day, year hours:minutes:seconds"
示例代碼:
vardate = getDate();//返回當(dāng)前時(shí)間對(duì)象date = getDate(1500000000000);// Fri Jul 14 2017 10:40:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)date = getDate('2017-7-14');// Fri Jul 14 2017 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)date = getDate(2017,6,14,10,40,0,0);// Fri Jul 14 2017 10:40:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
屬性
constructor:返回字符串 “Date”孵户。
方法
parse
UTC
now
toString
toDateString
toTimeString
toLocaleString
toLocaleDateString
toLocaleTimeString
valueOf
getTime
getFullYear
getUTCFullYear
getMonth
getUTCMonth
getDate
getUTCDate
getDay
getUTCDay
getHours
getUTCHours
getMinutes
getUTCMinutes
getSeconds
getUTCSeconds
getMilliseconds
getUTCMilliseconds
getTimezoneOffset
setTime
setMilliseconds
setUTCMilliseconds
setSeconds
setUTCSeconds
setMinutes
setUTCMinutes
setHours
setUTCHours
setDate
setUTCDate
setMonth
setUTCMonth
setFullYear
setUTCFullYear
toUTCString
toISOString
toJSON
以上方法的具體使用請(qǐng)參考ES5標(biāo)準(zhǔn)。
regexp
語(yǔ)法
生成 regexp 對(duì)象需要使用getRegExp函數(shù)岔留。
getRegExp(pattern[, flags])
參數(shù):
pattern: 正則表達(dá)式的內(nèi)容夏哭。
flags:修飾符。該字段只能包含以下字符:
g: global
i: ignoreCase
m: multiline献联。
示例代碼:
vara = getRegExp("x","img");console.log("x"=== a.source);console.log(true=== a.global);console.log(true=== a.ignoreCase);console.log(true=== a.multiline);
屬性
constructor:返回字符串"RegExp"竖配。
source
global
ignoreCase
multiline
lastIndex
除constructor外屬性的具體含義請(qǐng)參考ES5標(biāo)準(zhǔn)。
方法
exec
test
toString
以上方法的具體使用請(qǐng)參考ES5標(biāo)準(zhǔn)里逆。
數(shù)據(jù)類型判斷
constructor屬性
數(shù)據(jù)類型的判斷可以使用constructor屬性进胯。
示例代碼:
varnumber =10;console.log("Number"=== number.constructor );varstring ="str";console.log("String"=== string.constructor );varboolean =true;console.log("Boolean"=== boolean.constructor );varobject = {};console.log("Object"=== object.constructor );varfunc =function(){};console.log("Function"=== func.constructor );vararray = [];console.log("Array"=== array.constructor );vardate = getDate();console.log("Date"=== date.constructor );varregexp = getRegExp();console.log("RegExp"=== regexp.constructor );
typeof
使用typeof也可以區(qū)分部分?jǐn)?shù)據(jù)類型。
示例代碼:
varnumber =10;varboolean =true;varobject = {};varfunc =function(){};vararray = [];vardate = getDate();varregexp = getRegExp();console.log('number'===typeofnumber );console.log('boolean'===typeofboolean );console.log('object'===typeofobject );console.log('function'===typeoffunc );console.log('object'===typeofarray );console.log('object'===typeofdate );console.log('object'===typeofregexp );console.log('undefined'===typeofundefined);console.log('object'===typeofnull);