May 18, 2021 WeChat Mini Program Development Document
There are currently several data types in the WXS language:
Number consists of two values: integer, and number.
var a = 10;
var PI = 3.141592653589793;
Please refer to the ES5 standard for the specific use of the above methods.
String is written in two ways:
'hello world';
"hello world";
Refer to the ES5 standard for specific meanings of properties other than constructor.
Please refer to the ES5 standard for the specific use of the above methods.
Boolean values have only two specific values: true and false.
Please refer to the ES5 standard for the specific use of the above methods.
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);
console.log("Object" === {k:"1",v:"2"}.constructor)
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 keywords can be used in function. The keyword currently only supports the following attributes:
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);
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 supports the following definitions:
var a = []; //生成一个新的空数组
a = [1,"2",{},function(){}]; //生成一个新的非空数组,数组元素可以是任何类型
Refer to the ES5 standard for specific meanings of properties other than constructor.
Please refer to the ES5 standard for the specific use of the above methods.
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]]]]])
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 (中国标准时间)
Please refer to the ES5 standard for the specific use of the above methods.
The getRegExp function is required to generate the regexp object.
getRegExp(pattern[, flags])
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);
Refer to the ES5 standard for specific meanings of properties other than constructor.
Please refer to the ES5 standard for the specific use of the above methods.
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 );
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 );