All Products
Search
Document Center

Mobile Platform as a Service:Data type

Last Updated:Apr 07, 2021

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

  1. const number = 10;
  2. console.log(number.constructor); // "Number"
  3. const string = "str";
  4. console.log(string.constructor); // "String"
  5. const boolean = true;
  6. console.log(boolean.constructor); // "Boolean"
  7. const object = {};
  8. console.log(object.constructor); // "Object"
  9. const func = function(){};
  10. console.log(func.constructor); // "Function"
  11. const array = [];
  12. console.log(array.constructor); // "Array"
  13. const date = getDate();
  14. console.log(date.constructor); // "Date"
  15. const regexp = getRegExp();
  16. console.log(regexp.constructor); // "RegExp"

typeof

  1. const num = 100;
  2. const bool = false;
  3. const obj = {};
  4. const func = function(){};
  5. const array = [];
  6. const date = getDate();
  7. const regexp = getRegExp();
  8. console.log(typeof num); // 'number'
  9. console.log(typeof bool); // 'boolean'
  10. console.log(typeof obj); // 'object'
  11. console.log(typeof func); // 'function'
  12. console.log(typeof array); // 'object'
  13. console.log(typeof date); // 'object'
  14. console.log(typeof regexp); // 'object'
  15. console.log(typeof undefined); // 'undefined'
  16. console.log(typeof null); // 'object'

String

Syntax

  1. 'hello alipay';
  2. "hello taobao";

ES6 syntax

  1. // A string template
  2. const a = 'hello';
  3. 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

  1. const num = 10;
  2. 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

  1. 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

  1. var o = {}; // Generate an empty object.
  2. // Generate an object that is not empty.
  3. o = {
  4. 'str': "str", // The key of the object can be a string.
  5. constVar: 2, // The key of the object can also be an identifier that complies with variable definition rules.
  6. val: {}, // The value of the object can be of any data type.
  7. };
  8. // Read the attributes of the object.
  9. console.log(1 === o['string']);
  10. console.log(2 === o.constVar);
  11. // Write the attributes of the object.
  12. o['string']++;
  13. o['string'] += 10;
  14. o.constVar++;
  15. o.constVar += 10;
  16. // Read the attributes of the object.
  17. console.log(12 === o['string']);
  18. console.log(13 === o.constVar);

ES6 syntax:

  1. // ES6 syntax is supported.
  2. let a = 2;
  3. o = {
  4. a, // The attributes of the object.
  5. b() {}, // The method of the object.
  6. };
  7. const { a, b, c: d, e = 'default'} = {a: 1, b: 2, c: 3}; // Destructure an object and assign default values.
  8. const {a, ...other} = {a: 1, b: 2, c: 3}; // Destructure an object and assign values.
  9. const f = {...others}; // Destructure an object.

Attribute

constructor: returns "Object".

  1. console.log("Object" === {a:2,b:"5"}.constructor);

Method

toString: returns the string "[object Object]".

Function

Syntax

  1. // Method 1: Declare a function
  2. function a (x) {
  3. return x;
  4. }
  5. // Method 2: Specify an expression for the function
  6. var b = function (x) {
  7. return x;
  8. };
  9. // Method 3: Create an arrow function
  10. const double = x => x * 2;
  11. function f(x = 2){} // Default function parameters.
  12. function g({name: n = 'xiaoming', ...other} = {}) {} // Destructure function parameters and assign values.
  13. function h([a, b] = []) {} // Destructure function parameters and assign values.
  14. // Create an anonymous function and a closure
  15. var c = function (x) {
  16. return function () { return x;}
  17. };
  18. var d = c(25);
  19. console.log(25 === d());

You can use the arguments keyword in a function.

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

Output:

  1. true
  2. true
  3. 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

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

Output:

  1. true
  2. true
  3. true

Array

Syntax

  1. var a = []; // An empty array.
  2. a = [5,"5",{},function(){}]; // An array that is not empty. The elements in the array can be of any data type.
  3. const [b, , c, d = 5] = [1,2,3]; // Destructure an array and assign default values.
  4. const [e, ...other] = [1,2,3]; // Destructure an array and assign values.
  5. 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.

  1. getDate()
  2. getDate(milliseconds)
  3. getDate(datestring)
  4. 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 the month 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

  1. let date = getDate(); // Return a date object whose value is the current time.
  2. date = getDate(1500000000000);
  3. // Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)
  4. date = getDate('2016-6-29');
  5. // Fri June 29 2016 00:00:00 GMT+0800 (China Standard Time)
  6. date = getDate(2017, 6, 14, 10, 40, 0, 0);
  7. // Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)

Regexp

Syntax

Call getRegExp function to generate a regexp object.

  1. getRegExp(pattern[, flags])

Field

  • pattern: content of regular expression.
  • flags: modifying signs. The flags can only contain the following characters:
    • g: global
    • i: ignoreCase
    • m: 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

  1. var reg = getRegExp("name", "img");
  2. console.log("name" === reg.source);
  3. console.log(true === reg.global);
  4. console.log(true === reg.ignoreCase);
  5. console.log(true === reg.multiline);