类型判断
typeof 用来显示该字段为什么类型,instanceof 判断字段是否为某种类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| let data = {}; console.log(typeof data); console.log(data instanceof Object);
let array = []; console.log(typeof array); console.log(data instanceof Array); console.log(data instanceof Object);
let name; console.log(typeof name);
let num = 5; console.log(typeof num);
let text = "文字"; console.log(typeof text);
|
字符串转义和模板字面量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| let text = '我的名字叫"顺溜"'; document.write(text); document.write("<br>");
let arr = "转义斜杠加t为\t制表符"; document.write(arr); document.write("<br>");
let gang = "斜杠青年\\"; document.write(gang); document.write("<br>");
let content = `你是${gang},嘿嘿`; document.write(content);
|
模板字面量嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| let data = [ { title: "shoshfioshdfioshdf", }, { title: "奥为哈佛为哦哦谁金佛is点击佛", }, { title: "熊师傅欧师傅山东你发货", }, ]; console.log(data.map((item) => item.title).join(" ")); function show() { return `<ul>${data.map((item) => `<li>${item.title}</li>`).join(" ")}</ul>`; } document.write(`${show()}`);
|
字符串函数基本使用
1
| <input type="text" name="password" /> <span id="error"></span>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| let string = "SongZhengXiang";
console.log(string.toUpperCase());
console.log(string.toLowerCase());
let pas = document.querySelector("[name = 'password']"); let span = document.querySelector("#error"); pas.addEventListener("keyup", function() { this.value = this.value.trim(); if (this.value.length < 5) { span.innerHTML = "密码小于5位数"; } else { span.innerHTML = ""; } });
for (let i = 0; i < string.length; i++) { let span = document.createElement("span"); span.innerHTML = string[i]; span.style.fontSize = (i + 1) * 5 + "px"; document.body.append(span); }
|
字符串截取
1 2 3 4 5 6 7 8 9
| let string = "songzhengxiang";
console.log(string.slice(1, 5));
console.log(string.substr(1, 5));
console.log(string.substring(1, 5));
|
字符串检索的几种技巧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| let string = "songzhengxiang";
if (string.indexOf("z", 2) !== -1) { console.log("indexOf找到了"); } else { console.log("indexOf没找到"); }
if (string.includes("s")) { console.log("includes找到了"); } else { console.log("includes没找到"); }
console.log(string.lastIndexOf("n"), "lasIndexOf");
console.log(string.startsWith("s"));
console.log(string.endsWith("g"));
let guanjian = ["关键词1", "关键词2"]; let content = "这是由关键词和关键词2组合成的句子"; let state = guanjian.some((item) => { return content.includes(item); }); if (state) { console.log("包含关键词"); } else { console.log("不包含关键词"); }
|
字符串替换关键字
1 2 3 4 5 6 7
| let guanjian = ["关键词1", "关键词2"]; let content = "这是由关键词1和关键词2组合成的句子";
content = content.replace("关键词", `66666`); console.log(content); document.write(content);
|
电话号模糊处理
1 2 3 4
| function muhu(phone) { return String(phone).replace(String(phone).slice(3, 7), "****"); } console.log(muhu(17655544884));
|
Boolean 值隐式转换
1 2 3 4 5 6 7 8 9 10 11 12 13
| let array = [];
console.log(Number(array)); console.log(Boolean(array));
console.log(Number(false));
console.log(Number(true));
let object = {}; console.log(Boolean(object));
|
显示转换 Boolean 类型
1 2 3 4
| let number = 0;
console.log(!!number); console.log(Boolean(number));
|
Number 类型声明和基本函数使用
1 2 3 4 5 6 7 8 9 10 11
| let count = 99.5; console.log(typeof count);
console.log(typeof count.toString());
console.log(count.toFixed(2));
console.log(Number.isInteger(count));
|
判断 NaN 类型
1 2 3 4
| let string = "songzheng"; console.log(2 / string);
console.log(Number.isNaN(2 / string));
|
Math 数学计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| let gard = [1, 15, 54, 98, 5, 43];
console.log(Math.max(...gard));
console.log(Math.min(...gard));
console.log(Math.ceil(5.005));
console.log(Math.floor(5.999));
console.log(Math.round(5.52));
|
Math.random 随机点名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| let namearray = ["张三", "李四", "王五", "赵六", "马奇"];
console.log(Math.random() * 5);
console.log(Math.floor(Math.random() * (5 + 1)));
console.log(2 + Math.floor(Math.random() * (5 + 1 - 2)));
let index = Math.floor(Math.random() * namearray.length); console.log(namearray[index]);
let a = Math.floor(1 + Math.random() * (3 + 1 - 1)); console.log(namearray[a]);
function random(arr, start = 1, end) { end = end ? end : arr.length; start--; let index = Math.floor(start + Math.random() * (end - start)); return arr[index]; }
console.log(random(namearray, 3, 4));
|
日期时间戳计算脚本执行时间
1 2 3 4 5 6 7 8 9 10
| let start = Date.now(); for (let a = 0; a < 10; a++) { console.log(a); } let end = Date.now(); console.log(end - start + "ms");
console.time("sss"); for (let a = 0; a < 100; a++) {} console.timeEnd("sss");
|
封装日期格式化函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| let date = new Date();
console.log(date.getFullYear());
console.log(date.getMonth() + 1);
console.log(date.getDate());
console.log(date.getHours());
console.log(date.getMinutes());
console.log(date.getSeconds());
function dateFormat(date, format = "YYYY-MM-DD HH:mm:ss") { const config = { YYYY: date.getFullYear(), MM: date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1, DD: date.getDate(), HH: date.getHours(), mm: date.getMinutes(), ss: date.getSeconds(), }; for (let key in config) { format = format.replace(key, config[key]); } return format; } console.log(dateFormat(date, "YYYY-MM-DD HH:mm:ss"));
|
使用 moment
1 2 3
| <script src="http://cdn.staticfile.org/moment.js/2.24.0/moment.js"></script>; console.log(moment().format("YYYY-MM-DD HH:mm:ss dddd")); console.log(moment().format("LLLL"));
|