W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
SJS 目前支持如下數(shù)據(jù)類型:
SJS 提供了 constructor 與 typeof 兩種方式判斷數(shù)據(jù)類型。
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"
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'
'hello alipay';
"hello taobao";
// 字符串模板
const a = 'hello';
const str = `${a} alipay`;
"String"
除 constructor 外屬性的具體含義請參考 ES5 標(biāo)準(zhǔn)。
具體使用請參考 ES5 標(biāo)準(zhǔn)。
const num = 10;
const PI = 3.141592653589793;
"Number"
具體使用請參考 ES5 標(biāo)準(zhǔn)。
布爾值只有兩個特定的值:true 和 false。
const a = true;
"Boolean"
具體使用請參考 ES5 標(biāo)準(zhǔn)。
var o = {}; // 生成一個新的空對象
// 生成一個新的非空對象
o = {
'str': "str", // 對象的 key 可以是字符串
constVar: 2, // 對象的 key 也可以是符合變量定義規(guī)則的標(biāo)識符
val: {}, // 對象的 value 可以是任何類型
};
// 對象屬性的讀操作
console.log(1 === o['string']);
console.log(2 === o.constVar);
// 對象屬性的寫操作
o['string']++;
o['string'] += 10;
o.constVar++;
o.constVar += 10;
// 對象屬性的讀操作
console.log(12 === o['string']);
console.log(13 === o.constVar);
// 支持
let a = 2;
o = {
a, // 對象屬性
b() {}, // 對象方法
};
const { a, b, c: d, e = 'default'} = {a: 1, b: 2, c: 3}; // 對象解構(gòu)賦值 & default
const {a, ...other} = {a: 1, b: 2, c: 3}; // 對象解構(gòu)賦值
const f = {...others}; // 對象解構(gòu)
constructor:返回值"Object"
console.log("Object" === {a:2,b:"5"}.constructor);
toString:返回字符串 "[object Object]"
。
// 方法 1:函數(shù)聲明
function a (x) {
return x;
}
// 方法 2:函數(shù)表達(dá)式
var b = function (x) {
return x;
};
// 方法 3:箭頭函數(shù)
const double = x => x * 2;
function f(x = 2){} // 函數(shù)參數(shù)默認(rèn)
function g({name: n = 'xiaoming', ...other} = {}) {} // 函數(shù)參數(shù)解構(gòu)賦值
function h([a, b] = []) {} // 函數(shù)參數(shù)解構(gòu)賦值
// 匿名函數(shù)、閉包
var c = function (x) {
return function () { return x;}
};
var d = c(25);
console.log(25 === d());
function 中可以使用 arguments
關(guān)鍵字。
var a = function(){
console.log(2 === arguments.length);
console.log(1 === arguments[0]);
console.log(2 === arguments[1]);
};
a(1,2);
輸出:
true
true
true
"Function"
toString:返回字符串 "[function Function]"
。
var f = function (a,b) { }
console.log("Function" === f.constructor);
console.log("[function Function]" === f.toString());
console.log(2 === f.length);
輸出:
true
true
true
var a = []; // 空數(shù)組
a = [5,"5",{},function(){}]; // 非空數(shù)組,數(shù)組元素可以是任何類型
const [b, , c, d = 5] = [1,2,3]; // 數(shù)組解構(gòu)賦值 & 默認(rèn)值
const [e, ...other] = [1,2,3]; // 數(shù)組解構(gòu)賦值
const f = [...other]; // 數(shù)組解構(gòu)
"Array"
除constructor外屬性的具體含義請參考 ES5 標(biāo)準(zhǔn)。
具體使用請參考 ES5 標(biāo)準(zhǔn)。
生成 date 對象需要使用 getDate
函數(shù), 返回一個當(dāng)前時間的對象。
getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])
constructor:返回值"Date
具體使用請參考 ES5 標(biāo)準(zhǔn)。
let date = getDate(); //返回當(dāng)前時間對象
date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
date = getDate('2016-6-29');
// Fri June 29 2016 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
date = getDate(2017, 6, 14, 10, 40, 0, 0);
// Fri Jul 14 2017 10:40:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
生成 regexp 對象需要使用 getRegExp 函數(shù)。
getRegExp(pattern[, flags])
g
、i
、m
"RegExp"
。除 constructor 外屬性的具體含義請參考 ES5 標(biāo)準(zhǔn)。
具體使用請參考 ES5 標(biāo)準(zhǔn)。
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);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: