SJS supports the following data types:
- String: String
- Boolean: Boolean
- Number: Number
- Object: Object
- Function: Function
- Array: Array
- Date: Date
- Regexp: Regular expression
Obtain the data type
SJS provides constructor
and typeof
for you to obtain the data type.
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
Syntax
'hello alipay';
"hello taobao";
ES6 syntax
// A string template
const a = 'hello';
const str = `${a} alipay`;
Attribute
constructor
: returns"String"
.length
Note: For definitions of other attributes than
constructor
, see the ES6 specification.Method
- toString
- valueOf
- charAt
- charCodeAt
- concat
- indexOf
- lastIndexOf
- localeCompare
- match
- replace
- search
- slice
- split
- substring
- toLowerCase
- toLocaleLowerCase
- toUpperCase
- toLocaleUpperCase
- trim
Note: For information about how to use these global attributes, see the ES6 specification.
Number
Syntax
const num = 10;
const PI = 3.141592653589793;
Attribute
constructor
: returns "Number"
.
Method
- toString
- toLocaleString
- valueOf
- toFixed
- toExponential
- toPrecision
Note: For information about how to use these global attributes, see the ES6 specification.
Boolean
A boolean value is either true
or false
.
Syntax
const a = true;
Attribute
constructor
: returns"Boolean"
.
Method
- toString
- valueOf
Note: For information about how to use these global attributes, see the ES6 specification.
Object
Syntax
var o = {}; // Generate an empty object.
// Generate an object that is not empty.
o = {
'str': "str", // The key of the object can be a string.
constVar: 2, // The key of the object can also be an identifier that complies with variable definition rules.
val: {}, // The value of the object can be of any data type.
};
// Read the attributes of the object.
console.log(1 === o['string']);
console.log(2 === o.constVar);
// Write the attributes of the object.
o['string']++;
o['string'] += 10;
o.constVar++;
o.constVar += 10;
// Read the attributes of the object.
console.log(12 === o['string']);
console.log(13 === o.constVar);
ES6 syntax:
// ES6 syntax is supported.
let a = 2;
o = {
a, // The attributes of the object.
b() {}, // The method of the object.
};
const { a, b, c: d, e = 'default'} = {a: 1, b: 2, c: 3}; // Destructure an object and assign default values.
const {a, ...other} = {a: 1, b: 2, c: 3}; // Destructure an object and assign values.
const f = {...others}; // Destructure an object.
Attribute
constructor
: returns "Object"
.
console.log("Object" === {a:2,b:"5"}.constructor);
Method
toString: returns the string "[object Object]"
.
Function
Syntax
// Method 1: Declare a function
function a (x) {
return x;
}
// Method 2: Specify an expression for the function
var b = function (x) {
return x;
};
// Method 3: Create an arrow function
const double = x => x * 2;
function f(x = 2){} // Default function parameters.
function g({name: n = 'xiaoming', ...other} = {}) {} // Destructure function parameters and assign values.
function h([a, b] = []) {} // Destructure function parameters and assign values.
// Create an anonymous function and a closure
var c = function (x) {
return function () { return x;}
};
var d = c(25);
console.log(25 === d());
You can use the arguments
keyword in a function.
var a = function(){
console.log(2 === arguments.length);
console.log(1 === arguments[0]);
console.log(2 === arguments[1]);
};
a(1,2);
Output:
true
true
true
Attribute
constructor
: returns"Function"
.length
: returns the number of formal parameters in a function.
Method
toString: returns a string in the "[function Function]"
format.
Example
var f = function (a,b) { }
console.log("Function" === f.constructor);
console.log("[function Function]" === f.toString());
console.log(2 === f.length);
Output:
true
true
true
Array
Syntax
var a = []; // An empty array.
a = [5,"5",{},function(){}]; // An array that is not empty. The elements in the array can be of any data type.
const [b, , c, d = 5] = [1,2,3]; // Destructure an array and assign default values.
const [e, ...other] = [1,2,3]; // Destructure an array and assign values.
const f = [...other]; // Destructure an array.
Attribute
constructor
: returns"Array"
.length
Note: For definitions of other attributes than
constructor
, see the ES6 specification.Method
- toString
- concat
- join
- pop
- push
- reverse
- shift
- slice
- sort
- splice
- unshift
- indexOf
- lastIndexOf
- every
- some
- forEach
- map
- filter
- reduce
- reduceRight
Note: For information about how to use these global attributes, see the ES6 specification.
Date
Syntax
To generate a date object, use getDate
function. The function returns a date object whose value is the current time.
getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])
Field
milliseconds
: returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.datestring
: returns a string in themonth day, year hours:minutes:seconds
format.
Attribute
constructor
: returns "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
Note: For information about how to use these global attributes, see the ES6 specification.
Example
let date = getDate(); // Return a date object whose value is the current time.
date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)
date = getDate('2016-6-29');
// Fri June 29 2016 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)
Regexp
Syntax
Call getRegExp
function to generate a regexp object.
getRegExp(pattern[, flags])
Field
- pattern: content of regular expression.
- flags: modifying signs. The flags can only contain the following characters:
g
: globali
: ignoreCasem
: multiline
Attribute
- constructor: returns the string
"RegExp"
. - global
- ignoreCase
- lastIndex
- multiline
- source
Note: For definitions of other attributes than
constructor
, see the ES6 specification.Method
- exec
- test
- toString
Note: For information about how to use these global attributes, see the ES6 specification.
Example
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);