注冊

微信小程序數(shù)據(jù)類型,微信小程序WXS語言數(shù)據(jù)類型

2020-09-27
導(dǎo)讀:數(shù)據(jù)類型 WXS 語言目前共有以下幾種數(shù)據(jù)類型: number: 數(shù)值 string:字符串 boolean:布爾值 object:對象 function:函數(shù) array: 數(shù)組 date:日期 regexp:正則 number 語法 number 包括兩種數(shù)值:整...

數(shù)據(jù)類型

WXS 語言目前共有以下幾種數(shù)據(jù)類型:

微信小程序數(shù)據(jù)類型,微信小程序WXS語言數(shù)據(jù)類型

  • number : 數(shù)值
  • string :字符串
  • boolean:布爾值
  • object:對象
  • 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
以上方法的具體使用請參考 ES5 標(biāo)準(zhǔn)。

string

語法

string 有兩種寫法:

'hello world';
"hello world";

屬性

  • constructor:返回字符串 "String"。
  • length
除constructor外屬性的具體含義請參考 ES5 標(biāo)準(zhǔn)。

方法

  • toString
  • valueOf
  • charAt
  • charCodeAt
  • concat
  • indexOf
  • lastIndexOf
  • localeCompare
  • match
  • replace
  • search
  • slice
  • split
  • substring
  • toLowerCase
  • toLocaleLowerCase
  • toUpperCase
  • toLocaleUpperCase
  • trim
以上方法的具體使用請參考 ES5 標(biāo)準(zhǔn)。

boolean

語法

布爾值只有兩個特定的值:true 和 false。

屬性

  • constructor:返回字符串 "Boolean"。

方法

  • toString
  • valueOf
以上方法的具體使用請參考 ES5 標(biāo)準(zhǔn)。

object

語法

object 是一種無序的鍵值對。使用方法如下所示:

var o = {} //生成一個新的空對象

//生成一個新的非空對象
o = {
  'string'  : 1,  //object 的 key 可以是字符串
  const_var : 2,  //object 的 key 也可以是符合變量定義規(guī)則的標(biāo)識符
  func      : {}, //object 的 value 可以是任何類型
};

//對象屬性的讀操作
console.log(1 === o['string']);
console.log(2 === o.const_var);

//對象屬性的寫操作
o['string']++;
o['string'] += 10;
o.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]"。

function

語法

function 支持以下的定義方式:

//方法 1
function a (x) {
  return x;
}

//方法 2
var b = function (x) { 
  return x;
}

function 同時也支持以下的語法(匿名函數(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ù)個數(shù)。
  • [index]: 通過 index 下標(biāo)可以遍歷傳遞給函數(shù)的每個參數(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ù)的形參個數(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 = [];      //生成一個新的空數(shù)組

a = [1,"2",{},function(){}];  //生成一個新的非空數(shù)組,數(shù)組元素可以是任何類型

屬性

  • constructor:返回字符串 "Array"。
  • length
除constructor外屬性的具體含義請參考 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
以上方法的具體使用請參考 ES5 標(biāo)準(zhǔn)。

date

語法

生成 date 對象需要使用 getDate函數(shù), 返回一個當(dāng)前時間的對象。

getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])
  • 參數(shù)milliseconds: 從1970年1月1日00:00:00 UTC開始計算的毫秒數(shù)datestring: 日期字符串,其格式為:"month day, year hours:minutes:seconds"

示例代碼:

var date = getDate(); //返回當(dāng)前時間對象

date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
date = getDate('2017-7-14');
// Fri Jul 14 2017 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
date = getDate(2017, 6, 14, 10, 40, 0, 0);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

屬性

  • 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
以上方法的具體使用請參考 ES5 標(biāo)準(zhǔn)。

regexp

語法

生成 regexp 對象需要使用 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外屬性的具體含義請參考 ES5 標(biāo)準(zhǔn)。

方法

  • exec
  • test
  • toString
以上方法的具體使用請參考 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 );

 

更多微信小程序開發(fā)教程,可以關(guān)注hi小程序。
重磅推薦:小程序開店目錄

第一部分:小商店是什么

第二部分:如何開通一個小商店

第三部分:如何登錄小商店

第四部分:開店任務(wù)常見問題

第五部分:小商店可以賣什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收貨/物流

第九部分:小程序怎么結(jié)算

第十部分:小程序客服

第十一部分:電商創(chuàng)業(yè)

第十二部分:小程序游戲開發(fā)