Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program data type


May 18, 2021 WeChat Mini Program Development Document


Table of contents


The data type

There are currently several data types in the WXS language:

  • number: The value
  • String: String
  • Boolean: Boolean: Boolean value
  • Object: Object
  • function: Function
  • array: Array
  • Date: Date
  • regexp: The rules

number

Grammar

Number consists of two values: integer, and number.

var a = 10;
var PI = 3.141592653589793;

Property

  • constructor: Returns the string "Number".

Method

  • Tostring
  • toLocaleString
  • valueOf
  • toFixed
  • toExponential
  • toPrecision
Please refer to the ES5 standard for the specific use of the above methods.

string

Grammar

String is written in two ways:

'hello world';
"hello world";

Property

  • constructor: Returns the string "String".
  • length
Refer to the ES5 standard for specific meanings of properties other than constructor.

Method

  • Tostring
  • valueOf
  • charAt
  • charCodeAt
  • Concat
  • indexOf
  • lastIndexOf
  • localeCompare
  • match
  • replace
  • search
  • slice
  • split
  • substring
  • toLowerCase
  • toLocaleLowerCase
  • toUpperCase
  • toLocaleUpperCase
  • trim
Please refer to the ES5 standard for the specific use of the above methods.

boolean

Grammar

Boolean values have only two specific values: true and false.

Property

  • constructor: Returns the string "Boolean".

Method

  • Tostring
  • valueOf
Please refer to the ES5 standard for the specific use of the above methods.

object

Grammar

object is a disordered pair of key values. Here's how to use it:

var o = {} //生成一个新的空对象

//生成一个新的非空对象
o = {
  'string'  : 1,  //object 的 key 可以是字符串
  const_var : 2,  //object 的 key 也可以是符合变量定义规则的标识符
  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);

Property

  • constructor: Returns the string "Object".
console.log("Object" === {k:"1",v:"2"}.constructor)

Method

  • toString: Returns the string "object Object".

function

Grammar

Function supports the following definitions:

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

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

Function also supports the following syntax (anonymous functions, closures, etc.):

var a = function (x) {
  return function () { return x;}
}

var b = a(100);
console.log( 100 === b() );

arguments

Arguments keywords can be used in function. The keyword currently only supports the following attributes:

  • length: The number of arguments passed to the function.
  • Index: Each argument passed to the function can be traversed by the index undersring.

Example code:

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);

Property

  • constructor: Returns the string "Function".
  • Length: Returns the number of parameters of the function.

Method

  • ToString: Returns the string "function Function".

Example code:

var func = function (a,b,c) { }

console.log("Function" === func.constructor);
console.log(3 === func.length);
console.log("[function Function]" === func.toString());

array

Grammar

Array supports the following definitions:

var a = [];      //生成一个新的空数组

a = [1,"2",{},function(){}];  //生成一个新的非空数组,数组元素可以是任何类型

Property

  • constructor: Returns the string "Array".
  • length
Refer to the ES5 standard for specific meanings of properties other than constructor.

Method

  • Tostring
  • Concat
  • join
  • pop
  • push
  • reverse
  • shift
  • slice
  • sort
  • splice
  • unshift
  • indexOf
  • lastIndexOf
  • every
  • some
  • Foreach
  • map
  • filter
  • reduce
  • reduceRight
Please refer to the ES5 standard for the specific use of the above methods.

date

Grammar

To generate a date object, you need to use the getDate function to return an object at the current time.

getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])
  • Parameter milliseconds: The number of milliseconds calculated from 00:00:00 UTC on January 1, 1970: date string, in the format: "month day, year hours: minutes:seconds"

Example code:

var date = getDate(); //返回当前时间对象

date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中国标准时间)
date = getDate('2017-7-14');
// Fri Jul 14 2017 00:00:00 GMT+0800 (中国标准时间)
date = getDate(2017, 6, 14, 10, 40, 0, 0);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中国标准时间)

Property

  • constructor: Returns the string "Date".

Method

  • 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
Please refer to the ES5 standard for the specific use of the above methods.

Regexp

Grammar

The getRegExp function is required to generate the regexp object.

getRegExp(pattern[, flags])
  • Argument: Pattern: The contents of a regular expression. f lags: modifiers. This field can only contain the following characters: g: globali: ignoreCasem: multiline.

Example code:

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);

Property

  • constructor: Returns the string "RegExp".
  • source
  • global
  • ignoreCase
  • multiline
  • lastIndex
Refer to the ES5 standard for specific meanings of properties other than constructor.

Method

  • Exec
  • test
  • Tostring
Please refer to the ES5 standard for the specific use of the above methods.

Data type judgment

The constructor property

The data type can be judged using the constructor property.

Example code:

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

You can also distinguish between some data types using typeof.

Example code:

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 );