支付寶小程序框架 SJS·數(shù)據(jù)類型

2020-09-18 10:28 更新

SJS 目前支持如下數(shù)據(jù)類型:

  • string:字符串
  • boolean:布爾值
  • number:數(shù)值
  • object:對象
  • function:函數(shù)
  • array:數(shù)組
  • date:日期
  • regexp:正則表達(dá)式

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

SJS 提供了 constructor 與 typeof 兩種方式判斷數(shù)據(jù)類型。

constructor

const number = 10;
console.log(number.constructor); // "Number"
const string = "str";
console.log(string.constructor); // "String"
const boolean = true;
console.log(boolean.constructor); // "Boolean"
const object = {};
console.log(object.constructor); // "Object"
const func = function(){};
console.log(func.constructor); // "Function"
const array = [];
console.log(array.constructor); // "Array"
const date = getDate();
console.log(date.constructor); // "Date"
const regexp = getRegExp();
console.log(regexp.constructor); // "RegExp"

typeof

const num = 100;
const bool = false;
const obj = {};
const func = function(){};
const array = [];
const date = getDate();
const regexp = getRegExp();
console.log(typeof num); // 'number'
console.log(typeof bool); // 'boolean'
console.log(typeof obj); // 'object'
console.log(typeof func); // 'function'
console.log(typeof array); // 'object'
console.log(typeof date); // 'object'
console.log(typeof regexp); // 'object'
console.log(typeof undefined); // 'undefined'
console.log(typeof null); // 'object'

string

語法

'hello alipay';
"hello taobao";

ES6 語法

// 字符串模板
const a = 'hello';
const str = `${a} alipay`;

屬性

  • 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)。

number

語法

const num = 10;
const PI = 3.141592653589793;

屬性

  • constructor:返回值"Number"

方法

  • toString
  • toLocaleString
  • valueOf
  • toFixed
  • toExponential
  • toPrecision

具體使用請參考 ES5 標(biāo)準(zhǔn)。

boolean

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

語法

const a = true;

屬性

  • constructor:返回值"Boolean"

方法

  • toString
  • valueOf

具體使用請參考 ES5 標(biāo)準(zhǔn)。

object

語法

var o = {}; // 生成一個新的空對象
// 生成一個新的非空對象
o = {
  'str': "str",  // 對象的 key 可以是字符串
  constVar: 2,  // 對象的 key 也可以是符合變量定義規(guī)則的標(biāo)識符
  val: {}, // 對象的 value 可以是任何類型
};
// 對象屬性的讀操作
console.log(1 === o['string']);
console.log(2 === o.constVar);
// 對象屬性的寫操作
o['string']++;
o['string'] += 10;
o.constVar++;
o.constVar += 10;
// 對象屬性的讀操作
console.log(12 === o['string']);
console.log(13 === o.constVar);

ES6 語法

// 支持
let a = 2;
o = { 
  a, // 對象屬性
  b() {}, // 對象方法
};
const { a, b, c: d, e = 'default'} = {a: 1, b: 2, c: 3}; // 對象解構(gòu)賦值 & default
const {a, ...other} = {a: 1, b: 2, c: 3}; // 對象解構(gòu)賦值
const f = {...others}; // 對象解構(gòu)

屬性

constructor:返回值"Object"

console.log("Object" === {a:2,b:"5"}.constructor);

方法

toString:返回字符串 "[object Object]"。

function

語法

// 方法 1:函數(shù)聲明
function a (x) {
  return x;
}
// 方法 2:函數(shù)表達(dá)式
var b = function (x) { 
  return x;
};
// 方法 3:箭頭函數(shù)
const double = x => x * 2;
function f(x = 2){} // 函數(shù)參數(shù)默認(rèn)
function g({name: n = 'xiaoming', ...other} = {}) {} // 函數(shù)參數(shù)解構(gòu)賦值
function h([a, b] = []) {} // 函數(shù)參數(shù)解構(gòu)賦值
// 匿名函數(shù)、閉包
var c = function (x) {
  return function () { return x;}
};
var d = c(25);
console.log(25 === d());

function 中可以使用 arguments 關(guān)鍵字。

var a = function(){
    console.log(2 === arguments.length);
    console.log(1 === arguments[0]);
    console.log(2 === arguments[1]);
};
a(1,2);

輸出:

true
true
true

屬性

  • constructor:返回值"Function"
  • length:返回函數(shù)的形參個數(shù)

方法

toString:返回字符串 "[function Function]"

示例

var f = function (a,b) { }
console.log("Function" === f.constructor);
console.log("[function Function]" === f.toString());
console.log(2 === f.length);

輸出:

true
true
true

array

語法

var a = [];      // 空數(shù)組
a = [5,"5",{},function(){}];  // 非空數(shù)組,數(shù)組元素可以是任何類型
const [b, , c, d = 5] = [1,2,3]; // 數(shù)組解構(gòu)賦值 & 默認(rèn)值
const [e, ...other] = [1,2,3]; // 數(shù)組解構(gòu)賦值
const f = [...other]; // 數(shù)組解構(gòu)

屬性

  • 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"

屬性

constructor:返回值"Date

方法

  • 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)。

示例

let date = getDate(); //返回當(dāng)前時間對象
date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
date = getDate('2016-6-29');
// Fri June 29 2016 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)時間)

regexp

語法

生成 regexp 對象需要使用 getRegExp 函數(shù)。

getRegExp(pattern[, flags])

參數(shù)

  • pattern: 正則的內(nèi)容。
  • flags:修飾符,只能包括一下字符: gi 、m

    屬性

  • constructor:返回字符串 "RegExp"。
  • global
  • ignoreCase
  • lastIndex
  • multiline
  • source

除 constructor 外屬性的具體含義請參考 ES5 標(biāo)準(zhǔn)。

方法

  • exec
  • test
  • toString

具體使用請參考 ES5 標(biāo)準(zhǔn)。

示例

var reg = getRegExp("name", "img");
console.log("name" === reg.source);
console.log(true === reg.global);
console.log(true === reg.ignoreCase);
console.log(true === reg.multiline);
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號