Data Type

The sjs currently supports the following data types:

  • string: string
  • boolean: boolean
  • number: number
  • object: object
  • function: function
  • array: array
  • date: date
  • regexp: Regular expression

Determine Data Type

The sjs provides the constructor and typeof methods to determine the data type.

constructor

copy
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

copy
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

Syntax

copy
'hello world';
"hello Mini Program";

es6 syntax

copy
// string template
const a = 'hello';
const str = `${a} world`;

Attribute

  • constructor: Return value "String"
  • length

For the specific meanings of attributes in addition to the constructor, see the ES5 standard.

Method

  • toString
  • valueOf
  • charAt
  • charCodeAt
  • concat
  • indexOf
  • lastIndexOf
  • localeCompare
  • match
  • replace
  • search
  • slice
  • split
  • substring
  • toLowerCase
  • toLocaleLowerCase
  • toUpperCase
  • toLocaleUpperCase
  • trim

For specific usage, see the ES5 standard.

number

Syntax

copy
const num = 10;
const PI = 3.141592653589793;

Attribute

  • constructor: Return value "Number"

Method

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

For specific usage, see the ES5 standard.

boolean

there are two specific Boolean values: true and false

Syntax

copy
const a = true;

Attribute

  • constructor: Return value "Boolean"

Method

  • toString
  • valueOf

For specific usage, see the ES5 standard.

object

Syntax

copy
var o = {}; // generate a new empty object
// generate a new nonempty object
o = {
  'str': "str",  // key of the object can be a string
  constVar: 2,  // key of the object can also be an identifier that meets the variable definition rules
  val: {}, // value of the object can be of any type
};
// read operation of the object attribute
console.log(1 === o['string']);
console.log(2 === o.constVar);
// write operation of the object attribute
o['string']++;
o['string'] += 10;
o.constVar++;
o.constVar += 10;
// read operation of the object attribute
console.log(12 === o['string']);
console.log(13 === o.constVar);

es6 syntax:

copy
// support
let a = 2;
o = { 
  a, // object attribute
  b() {}, // object method
};
const { a, b, c: d, e = 'default'} = {a: 1, b: 2, c: 3}; // object destructuring assignment & default
const {a, ...other} = {a: 1, b: 2, c: 3}; // object destructuring assignment
const f = {...others}; // object destructuring

Attribute

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

Method

  • toString: Return string "[object Object]"

function

Syntax

copy
// method 1: function declaration
function a (x) {
  return x;
}
// method 2: function expression
var b = function (x) { 
  return x;
};
// method 3: arrow function
const double = x => x * 2;
function f(x = 2){} // function default parameters
function g({name: n = 'Name Example', ...other} = {}) {} // function parameter destructuring assignment
function h([a, b] = []) {} // function parameter destructuring assignment
// Anonymous functions, closures
var c = function (x) {
  return function () { return x;}
};
var d = c(25);
console.log(25 === d());

The "arguments" keyword can be used in the function.

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

Output:

copy
true
true
true

Attribute

  • constructor: Return value "Function"
  • Length: return the number of formal parameters of the function

    Method
  • toString: Return string "[function Function]"

Example

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

Output:

copy
true
true
true

array

Syntax

copy
var a = [];      // empty array
a = [5,"5",{},function(){}];  // nonempty array, array element can be of any type
const [b, , c, d = 5] = [1,2,3]; // array destructuring assignment & default
const [e, ...other] = [1,2,3]; // array destructuring assignment
const f = [...other]; // array destructuring

Attribute

  • constructor: Return value "Array"
  • length

For the specific meanings of attributes in addition to the constructor, see the ES5 standard.

Method

  • toString
  • concat
  • join
  • pop
  • push
  • reverse
  • shift
  • slice
  • sort
  • splice
  • unshift
  • indexOf
  • lastIndexOf
  • every
  • some
  • forEach
  • map
  • filter
  • reduce
  • reduceRight

For specific usage, see the ES5 standard.

date

Syntax

To generate a date object, the getDate function is required, which returns an object of the current time.

copy
getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])

Parameter

  • milliseconds: Milliseconds since 00:00:00 UTC of January 1st, 1970
  • datestring: Date string, format: "month day, year hours:minutes:seconds"

    Attribute
  • constructor: Return value "Date"

Method

  • 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

For specific usage, see the ES5 standard.

Example

copy
let date = getDate(); // return the current time object
date = getDate(1500000000000);
date = getDate('2016-6-29');
date = getDate(2017, 6, 14, 10, 40, 0, 0);

regexp

Syntax

To generate a regexp object, the getRegExp function is required.

copy
getRegExp(pattern[, flags])

Parameter

  • pattern: Content of the regular expression
  • Flags: modifier Can contain the following characters only:
  • g: global
  • i: ignoreCase
  • m: multiline

Attribute

  • Constructor: Return string "RegExp"
  • global
  • ignoreCase
  • lastIndex
  • multiline
  • source

For the specific meanings of attributes in addition to the constructor, see the ES5 standard.

Method

  • exec
  • test
  • toString

For specific usage, see the ES5 standard.

Example

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