數據類型
WXS語言目前共有以下幾種數據類型:
- number :數值
- string :字串
- boolean:布林值
- object :對象
- function :函數
- array :數組
- date :日期
- regexp :正則
number
語法
number包括兩種數值:整數,小數。
js
var a = 10;
var PI = 3.141592653589793;内容
- constructor:返回字串'Number'
方法
toString;
toLocaleString;
valueOf;
toFixed;
toExponential;
toPrecision.
以上方法的具體使用請參攷ES5標準。
string
語法
string有兩種寫法:
js
'hello world';
"hello world";内容
constructor:返回字串'String'
length
除constructor外内容的具體含義請參攷ES5標準。
方法
- toString;
- valueOf;
- charAt;
- charCodeAt;
- concat;
- indexOf;
- lastIndexOf;
- localeCompare;
- match;
- replace;
- search;
- slice;
- split;
- substring;
- toLowerCase;
- toLocaleLowerCase;
- toUpperCase;
- toLocaleUpperCase;
- trim;
以上方法的具體使用請參攷ES5標準。
boolean
語法
布林值只有兩個特定的值:true和false
内容
- constructor:返回字串'Boolean'
方法
- toString;
- valueOf.
以上方法的具體使用請參攷ES5標準。
object
語法
object是一種無序的鍵值對。 使用方法如下所示:
js
var o = {} // Generates a new empty object.
// Generates a new non-empty object.
o = {
'string' : 1, //The object’s key can be a string.
const_var : 2, //The object’s key can also be an identifier that conforms to the variable definition rules.
func : {}, //The object’s value can be of any type.
};
//Object property read operation
console.log(1 === o['string']);
console.log(2 === o.const_var);
//Object property write operation
o['string']++;
o['string'] += 10;
o.const_var++;
o.const_var += 10;
//Object property read operation
console.log(12 === o['string']);
console.log(13 === o.const_var);内容
constructor:返回字串'Object'
js
console.log("Object" === {k:"1",v:"2"}.constructor)方法
toString:返回字串'[object Object]'
function
語法
function支持以下的定義管道:
js
//Method 1
function a (x) {
return x;
}
//Method 2
var b = function (x) {
return x;
}function同時也支持以下的語法(匿名函數,閉包等):
js
var a = function (x) {
return function () { return x;}
}
var b = a(100);
console.log( 100 === b() );arguments
function裡面可以使用arguments關鍵字。 該關鍵字現時只支持以下的内容:
- length: 傳遞給函數的參數個數;
- [index] 傳遞給函數的參數個數;
示例代碼:
js
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]'。
示例代碼:
js
var func = function (a,b,c) { }
console.log("Function" === func.constructor);
console.log(3 === func.length);
console.log("[function Function]" === func.toString());array
語法
array支持以下的定義管道:
js
var a = []; // Generates a new empty array.
a = [1,"2",{},function(){}]; // Generates a new non-empty array, with array elements of any type.内容
constructor:返回字串'Array'
length.
除constructor外内容的具體含義請參攷ES5標準。
方法
- toString
- concat
- join
- pop
- push
- reverse
- shift
- slice
- sort
- splice
- unshift
- indexOf
- lastIndexOf
- every
- some
- forEach
- map
- filter
- reduce
- reduceRight
以上方法的具體使用請參攷ES5標準。
date
語法
生成date對象需要使用getDate函數,返回一個當前時間的對象。
js
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'。
示例代碼:
js
var date = getDate(); //Returns the current time object.
date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)
date = getDate('2017-7-14');
// Fri Jul 14 2017 00:00:00 GMT+0800 (China Standard Time)
date = getDate(2017, 6, 14, 10, 40, 0, 0);
// Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)内容
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;
- setUTCMonth;
- setFullYear;
- setUTCFullYear;
- toUTCString;
- toISOString;
- toJSON;
以上方法的具體使用請參攷ES5標準。
regexp
語法
生成regexp對象需要使用getRegExp函數。
js
getRegExp(pattern[, flags])- 參數:
- pattern:規則運算式的內容
- flags:修飾符。 該欄位只能包含以下字元:
- g: global
- i: ignoreCase
- m: multiline
示例代碼:
js
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標準。
方法
- exec;
- test;
- toString.
以上方法的具體使用請參攷ES5標準。
數據類型判斷
constructor内容
數據類型的判斷可以使用constructor内容。
示例代碼:
js
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也可以區分部分數據類型。
示例代碼:
js
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 );