微信小程序 數(shù)據(jù)類型
數(shù)據(jù)類型
WXS 語言目前共有以下幾種數(shù)據(jù)類型:
number : 數(shù)值
string :字符串
boolean:布爾值
object:對(duì)象
function:函數(shù)
array : 數(shù)組
date:日期
regexp:正則
number
語法
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
語法
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
語法
布爾值只有兩個(gè)特定的值:true 和 false。
屬性
constructor:返回字符串 "Boolean"。
方法
toString
valueOf
以上方法的具體使用請(qǐng)參考 ES5 標(biāo)準(zhǔn)。
object
語法
object 是一種無序的鍵值對(duì)。使用方法如下所示:
var o = {} //生成一個(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
語法
function 支持以下的定義方式:
//方法 1
function a (x) {
return x;
}
//方法 2
var b = function (x) {
return x;
}
function 同時(shí)也支持以下的語法(匿名函數(shù),閉包等):
var a = function (x) {
return function () { return x;}
}
var b = a(100);
console.log( 100 === b() );
arguments
function 里面可以使用 arguments 關(guān)鍵詞。該關(guān)鍵詞目前只支持以下的屬性:
length: 傳遞給函數(shù)的參數(shù)個(gè)數(shù)。
[index]: 通過 index 下標(biāo)可以遍歷傳遞給函數(shù)的每個(gè)參數(shù)。
示例代碼:
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:返回函數(shù)的形參個(gè)數(shù)。
方法
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());
array
語法
array 支持以下的定義方式:
var a = []; //生成一個(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
語法
生成 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"
示例代碼:
var date = getDate(); //返回當(dāng)前時(shí)間對(duì)象
date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
date = getDate('2017-7-14');
// Fri Jul 14 2017 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
date = getDate(2017, 6, 14, 10, 40, 0, 0);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(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
語法
生成 regexp 對(duì)象需要使用 getRegExp函數(shù)。
getRegExp(pattern[, flags])
參數(shù):pattern: 正則表達(dá)式的內(nèi)容。flags:修飾符。該字段只能包含以下字符:g: globali: ignoreCasem: multiline。
示例代碼:
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:返回字符串 "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 屬性。
示例代碼:
var number = 10;
console.log( "Number" === number.constructor );
var string = "str";
console.log( "String" === string.constructor );
var boolean = true;
console.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
使用 typeof 也可以區(qū)分部分?jǐn)?shù)據(jù)類型。
示例代碼:
var number = 10;
var boolean = true;
var 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 );
中國· 上海

關(guān)鍵詞
辦公室:上海市浦東新區(qū)郭守敬路351號(hào)
CopyRight?2009-2019 上海谷谷網(wǎng)絡(luò)科技有限公司 All Rights Reserved. 滬ICP備11022482號(hào)-8
- top
- 在線咨詢
-
添加微信咨詢