当前位置: 首页 > news >正文

服务质量好的网站设计制作南宁隆安网站建设

服务质量好的网站设计制作,南宁隆安网站建设,wordpress关闭更新,电商知识基础文章目录 ECMAScript系列#xff1a;简介ECMAScript系列#xff1a;ES6新特性let 关键字const 关键字变量的解构赋值模板字符串简化对象写法箭头函数参数默认值rest 参数spread扩展运算符Symbol迭代器生成器PromiseSetMapclass类数值扩展对象扩展模块化 ECMAScript系列#… 文章目录 ECMAScript系列简介ECMAScript系列ES6新特性let 关键字const 关键字变量的解构赋值模板字符串简化对象写法箭头函数参数默认值rest 参数spread扩展运算符Symbol迭代器生成器PromiseSetMapclass类数值扩展对象扩展模块化 ECMAScript系列ES7新特性Array.prototype.includes指数操作符 ECMAScript系列ES8新特性async 和 awaitasync 函数await 表达式Object.values 和 Object.entriesObject.getOwnPropertyDescriptors ECMAScript系列ES9新特性Rest/Spread 属性正则表达式命名捕获组正则表达式反向断言正则表达式 dotAll 模式 ECMAScript系列ES10新特性Object.fromEntriestrimStart 和 trimEndArray.prototype.flat 与 flatMapSymbol.prototype.description ECMAScript系列ES11新特性String.prototype.matchAll类的私有属性Promise.allSettled可选链操作符动态 import 导入BitIntglobalThis 对象 ECMAScript系列简介 ECMAEuropean Computer Manufacturers Association中文名称为欧洲计算机制 造商协会这个组织的目标是评估、开发和认可电信和计算机标准。1994 年后该 组织改名为 Ecma 国际。 ECMAScript 是由 Ecma 国际通过 ECMA-262 标准化的脚本程序设计语言。 Ecma 国际制定了许多标准而 ECMA-262 只是其中的一个所有标准列表查看 http://www.ecma-international.org/publications/standards/Standard.htm http://kangax.github.io/compat-table/es6/可查看兼容性 ECMAScript系列ES6新特性 let 关键字 let 关键字用来声明变量使用 let 声明的变量有几个特点 不允许重复声明 块儿级作用域 不存在变量提升 不影响作用域链 !DOCTYPE html html langenheadmeta charsetUTF-8title01-let/title /headbodyscript//声明变量let a;let b, c, d;let e 100;let f 521, g iloveyou, h [];//1.变量名不能重复声明let star 罗志祥;//let star小猪; //Uncaught SyntaxError: Identifier star has already been declared//2.块级作用域 es5:全局函数eval{let gril 周扬青;}//console.log(girl); Uncaught ReferenceError: girl is not defined //3.不存在变量提升//console.log(song); Uncaught ReferenceError: Cannot access song before initialization let song 恋爱达人;//4.不影响作用域链{let school 清华;function fn() {console.log(school);}fn();}/script /body/html案例 !DOCTYPE html html langenheadmeta charsetUTF-8titleDocument/titlestyle.item {border: 1px solid blue;width: 100px;height: 50px;margin: 10px;float: left;}/style /headbodydiv classcontainerh2 classpage-header点击切换颜色/h2div classitem/divdiv classitem/divdiv classitem/div/divscript//获取div元素对象let items document.getElementsByClassName(item);//遍历并绑定事件for (let index 0; index items.length; index) {const element items[index];element.onclick function () {//修改当前元素的背景颜色element.style.background pink;}}/script /body/htmlconst 关键字 const 关键字用来声明常量const 声明有以下特点 声明必须赋初始值 标识符一般为大写 不允许重复声明 值不允许修改 块儿级作用域 **注意**对象属性修改和数组元素变化不会出发const错误 **应用场景**声明对象类型使用const非对象类型声明选择let !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title03-const/title /headbodyscript//声明常量const SCHOOL 清华;//1.一定要赋初始值//const A;//Uncaught SyntaxError: Missing initializer in const declaration//2.一般常量使用大写(潜规则)const a 100;//3.常量的值不能修改//SCHOOLQINGHUA; //03-const.html:19 Uncaught TypeError: Assignment to constant variable. //4.块级作用域{const PLAYER UZI;}//console.log(PLAYER);//Uncaught ReferenceError: PLAYER is not defined//5.对于数组合对象的元素修改不算做对常量的修改不会报错const TEAM [UZI, MXLG, Ming];TEAM.push(Meiko);console.log(TEAM);/script /body/html变量的解构赋值 ES6 允许按照一定模式从数组和对象中提取值对变量进行赋值这被称为解构赋值。 注意频繁使用对象方法、数组元素就可以使用解构赋值形式 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title04-解构赋值/title /headbodyscript//es6 允许按照一定的模式从数组和对象中提取值对变量进行赋值。被称之为解构赋值//1.数组的结构const F4 [小沈阳, 刘能, 赵四, 宋小宝];let [xiao, liu, zhao, song] F4;console.log(xiao); //小沈阳console.log(liu);//刘能console.log(zhao);//赵四console.log(song);//宋小宝//对象的解构const zhaobenshan {name: 赵本山,age: 不详,xiaopin: function () {console.log(我可以演小品);}};let { name, age, xiaopin } zhaobenshan; console.log(name);//赵本山console.log(age);//不详console.log(xiaopin);//f(){...}/script /body/html模板字符串 模板字符串template string是增强版的字符串用反引号标识特点 字符串中可以出现换行符 可以使用 ${xxx} 形式输出变量 注意当遇到字符串与变量拼接的情况使用模板字符串 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title05-模板字符串/title /headbodyscript//ES6引入新的声明字符串的方式 反引号【】 //1.声明const str 我是一个字符串;console.log(str, typeof str);//我是一个字符串 string//2.内容中可以直接出现换行符const str1 ulli沈腾/lili玛丽/li/ul;console.log(str1);//3.变量拼接let lovest 肖战;let out ${lovest}是个很酷的演员;console.log(out);/script /body/html简化对象写法 ES6 允许在大括号里面直接写入变量和函数作为对象的属性和方法。这样的书写更加简洁。 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title06-简化对象写法/title /headbodyscript//ES6 允许在大括号里面直接写入变量和函数作为对象的属性和方法。这样的书写更加简洁let name 清华;let change function () {console.log(i can change you.);}const school {name, change, improve() {console.log(函数省略function);}};console.log(school);/*change: ƒ ()improve: ƒ improve()name: 清华*//script /body/html箭头函数 箭头函数的注意点: 如果形参只有一个则小括号可以省略 函数体如果只有一条语句则花括号可以省略函数的返回值为该条语句的执行结果 箭头函数 this 指向声明时所在作用域下 this 的值 箭头函数不能作为构造函数实例化 不能使用 arguments 注意箭头函数不会更改this指向用来指定回调函数会非常合适 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title07-箭头函数/title /headbodyscript//ES6 允许使用[箭头]定义函数//声明一个函数let fn function () {}let fn1 (a, b) {return a b;}//调用函数let result fn1(1, 2);console.log(result);//1.this 是静态的. this始终指向函数声明时所在作用域下的 this 的值function getName() {console.log(this.name);}let getName1 () {console.log(this.name);}//设置 window 对象的 name 属性window.name 清华;const school { name: qinghua };//直接调用getName(); //清华getName1(); //清华//call 方法调用getName.call(school); //qinghuagetName1.call(school); //清华//2.不能作为构造函数实例化对象// let Person (name, age) {// this.name name;// this.age age;// }//let me new Person(xiao, 30);//console.log(me);//Uncaught TypeError: Person is not a constructor//3.不能使用 arguments 变量// let func () {// console.log(arguments);// }//func();//Uncaught ReferenceError: arguments is not defined//4.箭头函数的简写//1)省略小括号当形参有且只有一个的时候let add n {return n n;}console.log(add(9)); //18//2)省略花括号当代码体只有一条语句的时候,此时 return 必须省略//而且语句的执行结果就是函数的返回值let pow n n * n;console.log(pow(9));//81/script /body/html案例 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title08-箭头函数实践/titlestylediv {width: 200px;height: 200px;background: #580;}/style /headbodydiv idbox/divscript//需求1点击div 2s后改变颜色let box document.getElementById(box);// box.addEventListener(click, function () {// //保存 this 的值;// let _this this;// //定时器// setTimeout(function () {// //修改背景颜色 this// _this.style.background red;// }, 2000)// })box.addEventListener(click, function () {setTimeout(() {this.style.background red;})})//需求2从数组中返回偶数的元素const arr [1, 6, 9, 10, 100, 25];// const result arr.filter(function (item) {// if (item % 2 0) {// return true;// }// else {// return false;// }// })console.log(result);//[6, 10, 100]// const result arr.filter(item {// if (item % 2 0) return true;// else return false;// })const result arr.filter(item item % 2 0);//箭头函数适合于与 this 无关的回调. 定时器数组的方法回调//箭头函数不适合与 this 有关的回调. 比如事件回调对象的方法/script /body/html参数默认值 ES6 允许给函数参数赋初始值 1形参初始值 具有默认值的参数一般位置要靠后(潜规则) 2与解构赋值结合 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title09-参数默认值/title /headbodyscript//ES6 允许给函数参数赋初始值//1.形参初始值 具有默认值的参数一般位置要靠后(潜规则)function add(a, b, c 10) {return a b c;}let result add(1, 2);console.log(result); //13 //如果没有赋初始值NaN//2.与解构赋值结合function connect({ host 127.0.0.1, username, password, port }) {console.log(host);console.log(username);console.log(password);console.log(port);}connect({ host: localhost, username: root, password: root, port: 3306 });/script /body/htmlrest 参数 ES6 引入 rest 参数用于获取函数的实参用来代替 arguments 注意rest 参数非常适合不定个数参数函数的场景 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title10-rest参数/title /headbodyscript//ES6 引入 rest 参数. 用于获取参数的实参用来代替 arguments//ES5 获取实参的方式function date() {console.log(arguments);}date(1, 2, 3);/*0: 11: 22: 3callee: ƒ date()length: 3Symbol(Symbol.iterator): ƒ values()__proto__: Object*///rest 参数function date1(...args) {console.log(args);}date1(1, 2, 3); //[1, 2, 3]//rest 参数必须要放在参数最后function fn(a, b, ...args) {console.log(a);console.log(b);console.log(args);}fn(1,2,3,4,5,6); //1 2 [3, 4, 5, 6]/script /body/htmlspread扩展运算符 扩展运算符spread也是三个点…。它好比 rest 参数的逆运算将一个数组转为用逗号分隔的参数序列对数组进行解包。 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title11-spread扩展运算符/title /headbodyscript//【...】 扩展运算符能将【数组】转换为逗号分隔的【参数序列】//声明一个数组const tfboys [易烊千玺, 王源, 王俊凯];//声明一个函数function chunwan() {console.log(arguments);}chunwan(...tfboys);//chunwan(易烊千玺,王源,王俊凯)/script /body/html扩展运算符应用 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title12-扩展运算符应用/title /headbodydiv/divdiv/divdiv/divscript//1.数组的合并const kuaizi [王太利, 肖央];const fenghuang [曾毅, 玲花];//const zuixuanxiaopingguo kuaizi.concat(fenghuang);const zuixuanxiaopingguo[...kuaizi,...fenghuang];console.log(zuixuanxiaopingguo); //[王太利, 肖央,曾毅, 玲花]//2.数组的克隆const sanzhihua[E,G,M];const sanyecao[...sanzhihua];console.log(sanyecao); //[E,G,M]//3.将伪数组转为真正的数组const divsdocument.getElementsByTagName(div);console.log(divs); //HTMLCollection(3) [div, div, div]const divArr[...divs];console.log(divArr); //[div, div, div]/script /body/htmlSymbol ES6 引入了一种新的原始数据类型 Symbol表示独一无二的值。它是 JavaScript 语言的第七种数据类型是一种类似于字符串的数据类型。 Symbol 特点 Symbol 的值是唯一的用来解决命名冲突的问题 Symbol 值不能与其他数据进行运算 Symbol 定义 的 对象属 性 不能 使 用 for…in 循 环遍 历 但 是可 以 使 用Reflect.ownKeys 来获取对象的所有键名 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title13-symbol/title /headbodyscript//创建symbollet s Symbol();console.log(s, typeof s); //Symbol() symbollet s2 Symbol(清华);let s3 Symbol(清华);console.log(s2 s3); //falselet s4 Symbol.for(清华);console.log(s4, typeof s4);//Symbol(清华) symbollet s5 Symbol.for(清华);console.log(s4 s5); //true//不能与其他数据进行运算//let result s 100; //Cannot convert a Symbol value to a number//let results100; //Cannot convert a Symbol value to ...//let resultss; //Cannot convert a Symbol value to a ... /script /body/htmlsymbol创建对象属性 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title14-symbol创建对象属性/title /headbodyscript//向对象中添加方法 up downlet game {up(){},name:俄罗斯方块}let methods {up: Symbol(),down: Symbol()}game[methods.up] function () {console.log(我可以改变形状.);}game[methods.down] function () {console.log(我可以快速下降.);}console.log(game); /*name: 俄罗斯方块up: ƒ up()Symbol(): ƒ ()Symbol(): ƒ ()__proto__: Object*/let youxi{name:狼人杀,[Symbol(say)](){console.log(我可以发言);},[Symbol(see)](){console.log(我可以看见);}}console.log(youxi); /*name: 狼人杀Symbol(say): ƒ [Symbol(say)]()Symbol(see): ƒ [Symbol(see)]()*//script /body/html除了定义自己使用的 Symbol 值以外ES6 还提供了 11 个内置的 Symbol 值指向语言内部使用的方法。可以称这些方法为魔术方法因为它们会在特定的场景下自动执行。 内置值说明Symbol.hasInstance当其他对象使用 instanceof 运算符判断是否为该对象的实例时会调用这个方法Symbol.isConcatSpreadable对象的 Symbol.isConcatSpreadable 属性等于的是一个布尔值表示该对象用于 Array.prototype.concat()时是否可以展开。Symbol.species创建衍生对象时会使用该属性Symbol.match当执行 str.match(myObject) 时如果该属性存在会调用它返回该方法的返回值。Symbol.replace当该对象被 str.replace(myObject)方法调用时会返回该方法的返回值。Symbol.search当该对象被 str.search (myObject)方法调用时会返回该方法的返回值。Symbol.split当该对象被 str.split(myObject)方法调用时会返回该方法的返回值。Symbol.iterator对象进行 for…of 循环时会调用 Symbol.iterator 方法返回该对象的默认遍历器Symbol.toPrimitive该对象被转为原始类型的值时会调用这个方法返回该对象对应的原始类型值。Symbol. toStringTag在该对象上面调用 toString 方法时返回该方法的返回值Symbol. unscopables该对象指定了使用 with 关键字时哪些属性会被 with环境排除。 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title15-symbol内置属性/title /headbodyscriptclass Person {static [Symbol.hasInstance](param) {console.log(param); //{ name:李四}console.log(我被用来检测类型了);}}let o { name: 李四 };console.log(o instanceof Person); //falseconst arr1 [1, 2, 3];const arr2 [4, 5, 6];arr2[Symbol.isConcatSpreadable] false;console.log(arr1.concat(arr2));//[1, 2, 3, [4, 5, 6]]/script /body/html迭代器 遍历器Iterator就是一种机制。它是一种接口为各种不同的数据结构提供统一的访问机制。任何数据结构只要部署 Iterator 接口就可以完成遍历操作。 1) ES6 创造了一种新的遍历命令 for…of 循环Iterator 接口主要供 for…of 消费 2) 原生具备 iterator 接口的数据(可用 for of 遍历) a) Array b) Arguments c) Set d) Map e) String f) TypedArray g) NodeList 3) 工作原理 a) 创建一个指针对象指向当前数据结构的起始位置 b) 第一次调用对象的 next 方法指针自动指向数据结构的第一个成员 c) 接下来不断调用 next 方法指针一直往后移动直到指向最后一个成员 d) 每调用 next 方法返回一个包含 value 和 done 属性的对象 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title16-迭代器/title /headbodyscript//声明一个数组const xiyou [唐僧, 孙悟空, 猪八戒, 沙僧];//使用for...of 遍历数组for (let v of xiyou) {console.log(v);}console.log(xiyou); // [唐僧, 孙悟空, 猪八戒, 沙僧]let iterator xiyou[Symbol.iterator]();//调用对象的next方法console.log(iterator.next()); //{value: 唐僧, done: false}console.log(iterator.next()); //{value: 孙悟空, done: false}console.log(iterator.next()); //{value: 猪八戒, done: false}console.log(iterator.next()); //{value: 沙僧, done: false}console.log(iterator.next()); //{value: undefined, done: true}/script /body/html迭代器应用案例 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title17-迭代器自定义遍历对象/title /headbodyscript//声明一个对象const banji {name: 尖子班,stus: [zhangsan,lisi,wangwu,zhaoliu],[Symbol.iterator]() {let index 0;let _this this;return {next: function () {if (index _this.stus.length) {const result { value: _this.stus[index], done: false };index;return result;}else return { value: undefined, done: true };}}}}//遍历这个对象for (let v of banji) { //Uncaught TypeError: banji is not iterableconsole.log(v); //}/script /body/html生成器 生成器函数是 ES6 提供的一种异步编程解决方案语法行为与传统函数完全不同 代码说明 的位置没有限制 生成器函数返回的结果是迭代器对象调用迭代器对象的 next 方法可以得到 yield 语句后的值 yield 相当于函数的暂停标记也可以认为是函数的分隔符每调用一次 next 方法执行一段代码 next 方法可以传递实参作为 yield 语句的返回值 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title18-生成器/title /headbodyscript//生成器其实就是一个特殊的函数//异步编程 纯回调函数//yield 函数代码的分隔符function* gen() {//console.log(111);yield 一只没有耳朵;//console.log(222);yield 一只没有尾巴;//console.log(333);yield 真奇怪;//console.log(444);}let iterator gen();//console.log(iterator); /*gen object*/console.log(iterator.next()); //{value: 一只没有耳朵, done: false}console.log(iterator.next()); //{value: 一只没有尾巴, done: false}console.log(iterator.next()); //{value: 真奇怪, done: false}console.log(iterator.next()); //{value: undefined, done: true}for(let v of gen()){console.log(v);}/script /body/html生成器函数参数 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title19-生成器函数参数/title /head bodyscriptfunction * gen(arg){console.log(arg);//param1let one yield 111;console.log(one); //param2let twoyield 222;console.log(two); //param3let threeyield 333;console.log(three); //param4}//执行获取迭代器对象let iteratorgen(param1);//next方法可以传入实参console.log(iterator.next()); //{value: 111, done: false}console.log(iterator.next(param2)); //{value: 222, done: false}console.log(iterator.next(param3)); //{value: 333, done: false}console.log(iterator.next(param4)); //{value: undefined, done: true}/script /body /html生成器函数实例 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title20-生成器函数实例/title /headbodyscript//异步编程 js本身是异步单线程 文件操作 网络操作//1s 后控制台输出111 2s 后输出222 3s 后输出333 //回调地狱// setTimeout(() {// console.log(111);// setTimeout(() {// console.log(222);// setTimeout(() {// console.log(333);// }, 3000)// }, 2000);// }, 1000);function one() {setTimeout(() {console.log(111);iterator.next();}, 1000);}function two() {setTimeout(() {console.log(222);iterator.next();}, 2000);}function three() {setTimeout(() {console.log(333);iterator.next();}, 3000);}function* gen() {yield one();yield two();yield three();}let iterator gen();iterator.next();/script /body/html生成器函数实例2 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title21-生成器函数实例2/title /headbodyscript//模拟获取 用户数据 订单数据 商品数据function getUsers() {setTimeout(() {let data 用户数据;//调用next方法并且将数据传入iterator.next(data);}, 1000)}function getOrders() {setTimeout(() {let data 订单数据;iterator.next(data);}, 1000)}function getGoods() {setTimeout(() {let data 商品数据;iterator.next(data);}, 1000)}function * gen() {let users yield getUsers();console.log(users);let orders yield getOrders();console.log(orders);let goods yield getGoods();console.log(goods);}let iterator gen();iterator.next();/script /body/htmlPromise Promise 是 ES6 引入的异步编程的新解决方案。语法上 Promise 是一个构造函数 用来封装异步操作并可以获取其成功或失败的结果。 Promise 构造函数: Promise (excutor) {} Promise.prototype.then 方法 Promise.prototype.catch 方法 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title22-promise基本语法/title /headbodyscript//实例化 Promise 对象const p new Promise(function (resolve, reject) {setTimeout(function () {// let data 数据库中的用户数据;// resolve(data);let err 数据读取失败;reject(err);}, 1000);})//调用 Promise 对象的 then方法p.then(function (value) {console.log(value); //数据库中的用户数据 }, function (reason) {console.log(reason);})/script /body/htmlPromise读取文件案例 // //1.引入fs模块 const fs require(fs);// //2.调用方法读取文件 // fs.readFile(./01-let.html,(err,data){ // //如果失败则抛出错误 // if(err) throw err; // //如果没有出错则输出内容 // console.log(data.toString()); // })//3.使用 Promise 封装 const p new Promise((resolve, reject) {fs.readFile(./01-let.htm, (err, data) {//判断如果失败if (err) reject(err);//如果成功resolve(data);}) })p.then(value {console.log(value.toString()); }, reason {console.log(reason); })Promise封装ajax !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlepromise封装ajax/title /headbodyscriptconst p new Promise(function (resolve, reject) {//1.创建对象const xhr new XMLHttpRequest();//2.初始化xhr.open(GET, https://api.apiopen.top/getJoke);//3.发送xhr.send();//4.绑定事件处理响应结果xhr.onreadystatechange function () {//判断if (xhr.readyState 4) {if (xhr.status 200 xhr.status 300) {resolve(xhr.response);}else reject(xhr.response);}}})//指定回调p.then((result) {console.log(result);}).catch((err) {console.log(err);});/script /body/htmlPromise.prototype.then方法 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title25-Promise.prototype.then/title /headbodyscript//创建 promise 对象const p new Promise((resolve, reject) {setTimeout(() {resolve(用户数据);}, 1000)})//调用 then 方法 then方法的返回结果是 promise 对象对象状态由回调函数执行结果决定//1.如果回调函数中返回的结果是 非 promise 类型的属性状态为成功返回值为对象的成功的值const result p.then(value {console.log(value);//1.非 promise 类型的属性//return 123; ///*[[PromiseState]]: fulfilled [[PromiseResult]]:123*///2.是 promise 对象// return new Promise((resolve,reject){// //resolve(ok); /*[[PromiseState]]: fulfilled [[PromiseResult]]: ok*/// reject(error); /* [[PromiseState]]: rejected [[PromiseResult]]: error*/// })throw new Error(error.);/*[[PromiseState]]: rejected [[PromiseResult]]: Error: error. at*/}, reason {console.warn(reason);})console.log(result);//链式调用p.then(value{}).then(value{});/script /body/htmlPromise.catch方法 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title26-promise-catch方法/title /head bodyscriptconst p new Promise((resolve,reject){setTimeout((){//设置 p 对象的状态为失败并设置失败的值reject(error);},1000)})p.then(value{},reason{console.warn(reason);})p.catch(reason{console.warn(reason);})/script /body /htmlpromise读取多个文件 //引入 fs 模块 const fs require(fs);// fs.readFile(./01-let.html, (err, data1) { // fs.readFile(./02-let实践案例.html, (err, data2) { // fs.readFile(./03-const.html, (err, data3) { // let resultdata1\r\ndata2\r\ndata3; // console.log(result); // }) // }) // })//使用 promise 对象 const p new Promise((resolve,reject){fs.readFile(./01-let.html,(err,data){resolve(data);}) })p.then(value{ return new Promise((resolve,reject){fs.readFile(./02-let实践案例.html,(err,data){resolve([value,data]);})}) }).then(value{return new Promise((resolve,reject){fs.readFile(./03-const.html,(err,data){value.push(data);resolve(value)})}) }).then(value{console.log(value.join(\r\n)); })Set ES6 提供了新的数据结构 Set集合。它类似于数组但成员的值都是唯一的集合实现了 iterator 接口所以可以使用『扩展运算符』和『for…of…』进行遍历集合的属性和方法 size 返回集合的元素个数 add 增加一个新元素返回当前集合 delete 删除元素返回 boolean 值 has 检测集合中是否包含某个元素返回 boolean 值 clear 清空集合返回 undefined !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title28-set/title /headbodyscript//声明一个 setlet s new Set();let s2 new Set([a, b, c, a]);console.log(s, typeof s); //Set(0)[[Entries]]No propertiessize: (...)__proto__: Set objectconsole.log(s2); //Set(3) {a, b, c}//元素的个数console.log(s2.size); //3//添加新的元素s2.add(d);console.log(s2); //Set(4) {a, b, c, d}//删除元素s2.delete(a);console.log(s2); //Set(3) {b, c, d}console.log(s2.has(a)); //falseconsole.log(s2.has(b)); //true//清空// s2.clear();// console.log(s2); //Set(0) {}for(let v of s2){console.log(v); // b c d}/script /body/htmlset实践 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title29-set实践/title /headbodyscriptlet arr [1, 2, 3, 4, 5, 4, 3, 2, 1];//1.数组去重// let result[...new Set(arr)];// console.log(result); //[1,2,3,4,5]//2.交集let arr2 [4, 5, 6, 5, 6];// let result[...new Set(arr)].filter(item{// let s2new Set(arr2);// if (s2.has(item)) {// return true;// }// else return false;// });let result[...new Set(arr)].filter(itemnew Set(arr2).has(item));console.log(result); //[4, 5]//3.并集let union [...new Set([...arr,...arr2])];console.log(union); //[1, 2, 3, 4, 5, 6]//4.差集let diff[...new Set(arr)].filter(item!(new Set(arr2).has(item)));console.log(diff); //[1, 2, 3]/script /body/htmlMap ES6 提供了 Map 数据结构。它类似于对象也是键值对的集合。但是“键”的范围不限于字符串各种类型的值包括对象都可以当作键。Map 也实现了iterator 接口所以可以使用『扩展运算符』和『for…of…』进行遍历。Map 的属性和方法 size 返回 Map 的元素个数 set 增加一个新元素返回当前 Map get 返回键名对象的键值 has 检测 Map 中是否包含某个元素返回 boolean 值 clear 清空集合返回 undefined !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title30-map/title /headbodyscript//声明 Maplet m new Map();//添加元素m.set(name, qinghua);m.set(func, function () {console.log(func);})m.set({ school: qinghua }, [北京, 中国]);console.log(m); /* Map(3) {name qinghua, func ƒ, {…} Array(2)}[[Entries]]0: {func function () { console.log(func); }}1: {Object Array(2)}size: (...)__proto__: Map*///sizeconsole.log(m.size); //3//删除m.delete(name); console.log(m); /*Map(2) {func ƒ, {…} Array(2)}[[Entries]]0: {func function () { console.log(func); }}key: funcvalue: ƒ ()1: {Object Array(2)}key: {school: qinghua}value: (2) [北京, 中国]size: (...)__proto__: Map*///获取console.log(m.get(func)); /* ƒ () {console.log(func);}*///清空//m.clear();for(let v of m){console.log(v); /* 0: {school: qinghua}1: (2) [北京, 中国]length: 2__proto__: Array(0)*/}/script /body/htmlclass类 ES6 提供了更接近传统语言的写法引入了 Class类这个概念作为对象的模板。通过 class 关键字可以定义类。基本上ES6 的 class 可以看作只是一个语法糖它的绝大部分功能ES5 都可以做到新的 class 写法只是让对象原型的写法更加清晰、更像面向对象程的语法而已。 知识点 class 声明类 constructor 定义构造函数初始化 extends 继承父类 super 调用父级构造方法 static 定义静态方法和属性 父类方法可以重写 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title31-类声明/title /headbodyscript/*//手机function Phone(brand,price){this.brandbrand;this.priceprice;}//添加方法Phone.prototype.callfunction(){console.log(我可以打电话);}let Huaweinew Phone(华为,9999);console.log(Huawei); /*Phonebrand: 华为price: 9999__proto__:call: ƒ ()constructor: ƒ Phone(brand,price)__proto__: Object*///es6class Phone {constructor(brand, price) {this.brand brand;this.price price;}//方法必须使用该语法不能使用 ES5 的对象完整形式call() {console.log(我可以打电话);}}let OnePlus new Phone();OnePlus.call(); //我可以打电话/script /body/html类的静态成员 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title静态成员/title /headbodyscript/*function Phone(){}Phone.name手机;Phone.changefunction(){console.log(我能改变自己);}Phone.prototype.size5;let nokianew Phone();console.log(nokia.name); //undefinedconsole.log(nokia.size); //5*/class Phone {static name 手机;static change() {console.log(我可以改变自己);}}let nokia new Phone();console.log(nokia.name); //undefinedconsole.log(Phone.name); //手机/script /body/htmles5实现继承 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title33-类继承/title /headbodyscript//手机function Phone(brand, price) {this.brand brand;this.price price;}Phone.prototype.call function () {console.log(我可以打电话);}//子级 智能手机function SmartPhone(brand, price, color, size) {Phone.call(this, brand, price);this.color color;this.size size;}//设置子级构造函数的原型SmartPhone.prototypenew Phone;SmartPhone.prototype.constructorSmartPhone;//声明子类的方法SmartPhone.prototype.photofunction(){console.log(我可以拍照);}SmartPhone.prototype.playGamefunction(){console.log(我可以玩游戏);}const chuizinew SmartPhone(锤子,3999,黑色,6);console.log(chuizi); /* {brand: 锤子,price: 3999,color: 黑色,size: 6}*//script /body/htmles6写法 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title34-类继承2/title /head bodyscriptclass Phone{//构造函数constructor(brand,price){this.brandbrand;this.priceprice;}call(){console.log(我可以打电话);}}class SmartPhone extends Phone{//沟站函数constructor(brand,price,color,size) {super(brand,price);//Phone.call(this,brand,price) this.colorcolor;this.sizesize;}photo(){console.log(拍照);}playGame(){console.log(玩游戏);}call(){console.log(我可以视频通话);}}const xiaominew SmartPhone(小米,799,黑色,5.5);xiaomi.call();//我可以视频通话 重写xiaomi.photo()//拍照console.log(xiaomi); /*{brand: 小米,price: 799,color: 黑色,size: 5.5}*//script /body /htmlget-set !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titleget和set/title /head bodyscript//get 和 setclass Phone{get price(){console.log(价格属性被读取了);}set price(newValue){console.log(价格属性被修改了); // 必须要有一个参数 Uncaught SyntaxError: Setter must have exactly one formal parameter.}}let snew Phone();console.log(s.price); //价格属性被读取了 undefineds.price10; //价格属性被修改了/script /body /html数值扩展 二进制和八进制 ES6 提供了二进制和八进制数值的新的写法分别用前缀 0b 和 0o 表示。 Number.isFinite() 与 Number.isNaN() Number.isFinite() 用来检查一个数值是否为有限的 Number.isNaN() 用来检查一个值是否为 NaN Number.parseInt() 与 Number.parseFloat() ES6 将全局方法 parseInt 和 parseFloat移植到 Number 对象上面使用不变。 Math.trunc 用于去除一个数的小数部分返回整数部分。 Number.isInteger Number.isInteger() 用来判断一个数值是否为整数 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title数值扩展/title /head bodyscript//0.Number.EPSILON 是 Javascript 表示的最小精度//EPSILON 属性的值接近于 2.220446049250313e-16console.log(Number.EPSILON);console.log(0.10.2); //0.30000000000000004function equal(a,b){if (Math.abs(a-b)Number.EPSILON) {return true;}else return false;}console.log(0.10.20.3); //falseconsole.log(equal(0.10.2,0.3));//true //1.二进制和八进制let b0b1010; //二进制console.log(b); //10let o0o777; //八进制console.log(o); //511let d100; //十进制console.log(d); //100let x0xff; //十六进制console.log(x); //255//2.Number.isFinite 检测一个数值是否为有限数console.log(Number.isFinite(100)); //trueconsole.log(Number.isFinite(100/0)); //falseconsole.log(Number.isFinite(Infinity)); //false//3.Number.isNaN 检测一个数值是否为 NaNconsole.log(Number.isNaN(123));//false//4.Number.parseInt Number.parseFloat字符串转整数、浮点数console.log(Number.parseInt(5201314love)); //5201314console.log(Number.parseFloat(3.1415926)); //3.1415926//5.Number.isInteger 判断一个数是否为整数console.log(Number.isInteger(5)); //trueconsole.log(Number.isInteger(2.3)); //false//6.Math.trunc 将数字的小数部分抹掉console.log(Math.trunc(3.5)); //3//7.Math.sign 判断一个数到底为正数 负数 还是0 console.log(Math.sign(100)); //1console.log(Math.sign(0)); //0console.log(Math.sign(-200)); //-1/script /body /html对象扩展 ES6 新增了一些 Object 对象的方法 Object.is 比较两个值是否严格相等与『』行为基本一致0 与 NaN Object.assign 对象的合并将源对象的所有可枚举属性复制到目标对象 proto、setPrototypeOf、 setPrototypeOf 可以直接设置对象的原型 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title37-对象方法扩展/title /headbodyscript//1.Object.is 判断两个值是否完全相等console.log(Object.is(120, 121)); //false console.log(Object.is(NaN, NaN)); //trueconsole.log(NaN NaN); //false//2.Object.assign 对象的合并const config1 {host: localhost,port: 3306}const config2 {host:http://mcode.cn,port: 3306,user:root}let result Object.assign(config1, config2);console.log(result); /* {host: http://mcode.cn,port: 3306,user: root}*///3.Object.setPrototypeOf 设置原型对象 Object.getPrototypeofconst school{name:清华};const cities{xiaoqu:[中国,北京]};Object.setPrototypeOf(school,cities);console.log(school); /* {name: 清华}name: 清华__proto__:xiaoqu: (2) [中国, 北京]__proto__: Object*/console.log(Object.getPrototypeOf(school)); /*{xiaoqu: [中国,北京]}*//script /body/html模块化 模块化是指将一个大的程序文件拆分成许多小的文件然后将小文件组合起来。 模块化的优势有以下几点 防止命名冲突 代码复用 高维护性 ES6 之前的模块化规范有 CommonJS NodeJS、Browserify AMD requireJS CMD seaJS 模块功能主要由两个命令构成export 和 import。 export 命令用于规定模块的对外接口 import 命令用于输入其他模块提供的功能 分别暴露 //分别暴露 export let code java;export function getCode() {console.log(code); }统一暴露 //统一暴露 let school 清华;function findJob() {console.log(找任务); }export { school, findJob }默认暴露 //默认暴露 export default {school:vmcode,change:function(){console.log(改变);} }import的几种方式 1.通用的导入方式 2.解构赋值形式 3.简便形式 只能针对默认暴露 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title38-ES6模块化/title /headbodyscript typemodule//1.通用的导入方式//引入 38-分别暴露.js 模块内容import * as m38 from ./38-分别暴露.js;console.log(m38); /*Module {Symbol(Symbol.toStringTag): Module}code: (...)getCode: (...)Symbol(Symbol.toStringTag): Moduleget code: ƒ ()set code: ƒ ()get getCode: ƒ ()set getCode: ƒ ()*///引入 38-统一暴露.jsimport * as m39 from ./38-统一暴露.js;console.log(m39); /*Module {Symbol(Symbol.toStringTag): Module}findJob: (...)school: (...)Symbol(Symbol.toStringTag): Moduleget findJob: ƒ ()set findJob: ƒ ()get school: ƒ ()set school: ƒ ()*///引入 38-默认暴露.jsimport * as m40 from ./38-默认暴露.js;console.log(m40); /*Module {Symbol(Symbol.toStringTag): Module}default: Objectchange: ƒ ()school: vmcode__proto__: ObjectSymbol(Symbol.toStringTag): Moduleget default: ƒ ()set default: ƒ ()*/m40.default.change(); //改变//2.解构赋值形式import {code,getCode} from ./38-分别暴露.js;import {school as qinghua,findJob} from ./38-统一暴露.js;import {default as default1} from ./38-默认暴露.jsconsole.log(code); //javaconsole.log(getCode); /*ƒ getCode() {console.log(code);}*/console.log(qinghua); //清华console.log(findJob); /*ƒ findJob() {console.log(找任务);}*/console.log(default1); /* school: vmcode, change: ƒ}*///3.简便形式 只能针对默认暴露import default2 from ./38-默认暴露.js;console.log(default2); /* school: vmcode, change: ƒ}*//scriptscript src./38-app.js typemodule/script /body/htmlbabel打包 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title /head bodyscript src./dist/bundle.js//1.安装工具 babel-cli babel-preset-env browserify(webpack)//2.npx babel ./js -d dist/js --presetsbabel-preset-env//4.打包 npx browserify dist/js/38-app.js -o dist/bundle.js/script /body /htmlnpm init --yes npm i babel-cli babel-preset-env browserify -D npx babel ./js -d dist/js --presetsbabel-preset-env npx browserify dist/js/38-app.js -o dist/bundle.jsECMAScript系列ES7新特性 Array.prototype.includes Includes 方法用来检测数组中是否包含某个元素返回布尔类型值 //includes indexOfconst arr[a,b,c];console.log(arr.includes(d)); // falseconsole.log(arr.includes(a)); //true指数操作符 在 ES7 中引入指数运算符「**」用来实现幂运算功能与 Math.pow 结果相同 //**console.log(2 ** 10); //1024console.log(Math.pow(2,10)); //1024ECMAScript系列ES8新特性 async 和 await async 和 await 两种语法结合可以让异步代码像同步代码一样 async 函数 async 函数的返回值为 promise 对象 promise 对象的结果由 async 函数执行的返回值决定 //async 函数async function fn() {//返回一个字符串//return code;//返回的结果不是一个 promise 类型的对象返回的结果就是成功 promise对象//return;//抛出错误返回的结果是一个失败的 promise//throw new Error(error);return new Promise((resolve, reject) {//resolve(success data.);//reject(fail data.)})}const result fn();console.log(result)result.then(value {console.log(value);}, reason {console.warn(reason)});await 表达式 await 必须写在 async 函数中 await 右侧的表达式一般为 promise 对象 await 返回的是 promise 成功的值 await 的 promise 失败了, 就会抛出异常, 需要通过 try…catch 捕获处理 //创建 promise 对象const p new Promise((resolve, reject) {//resolve(success data.)reject(fail data.);})//await 要放在 async 函数中.async function main() {try {let result await p;console.log(result)} catch (e) {console.log(e)}}//调用函数main();案例 //1.引入 fs 模块 const fs require(fs);function read1() {return new Promise((resolve, reject) {fs.readFile(./1-async.html, (err, data) {//如果失败if (err) {reject(err);} else resolve(data);});}) }function read2() {return new Promise((resolve, reject) {fs.readFile(./2-await.html, (err, data) {//如果失败if (err) {reject(err);} else resolve(data);});}) }//声明一个async 函数 async function main() {let readFile1 await read1();let readFile2 await read2();console.log(readFile1.toString());console.log(readFile2.toString()) }main();async和await封装ajax请求 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title封装ajax请求/title /headbodyscript//发送ajax请求返回的结果是 Promise 对象function sendAjax(url) {return new Promise((resolve, reject) {//1.创建对象const x new XMLHttpRequest();//2.初始化x.open(GET, url);//3.发送x.send();//4.事件绑定x.onreadystatechange () {if (x.readyState 4) {if (x.status 200 x.status 300) {resolve(x.response);}else {reject(x.status);}}}});}// promise 测试sendAjax(https://api.apiopen.top/getJoke).then(value {console.log(value);}, reason {console.log(reason);})// async 和 await 测试async function mian(){let result await sendAjax(https://api.apiopen.top/getJoke);console.log(result)}mian();/script /body/htmlObject.values 和 Object.entries Object.values()方法返回一个给定对象的所有可枚举属性值的数组 Object.entries()方法返回一个给定对象自身可遍历属性 [key,value] 的数组 //声明对象const school{name:清华,city:[北京],subject:[web,dotnet,java]}//获取对象所有的键console.log(Object.keys(school));// [name, city, subject]//获取对象所有的值console.log(Object.values(school)); //[清华, Array(1), Array(3)]//获取对象的键值对console.log(Object.entries(school)); // [Array(2), Array(2), Array(2)]const mnew Map(Object.entries(school));console.log(m);//Map(3) {name 清华, city Array(1), subject Array(3)}Object.getOwnPropertyDescriptors 该方法返回指定对象所有自身属性的描述对象 //对象属性的描述对象console.log(Object.getOwnPropertyDescriptors(school));/*city: {value: Array(1), writable: true, enumerable: true, configurable: true}name: {value: 清华, writable: true, enumerable: true, configurable: true}subject: {value: Array(3), writable: true, enumerable: true, configurable: true}__proto__: Object*/const objObject.create(null,{name:{//设置值value:清华,//属性特性writable:true,configurable:true,enumerable:true,}})ECMAScript系列ES9新特性 Rest/Spread 属性 Rest 参数与 spread 扩展运算符在 ES6 中已经引入不过 ES6 中只针对于数组 在 ES9 中为对象提供了像数组一样的 rest 参数和扩展运算符 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title对象展开/title /headbodyscript/* Rest 参数 与 spread 扩展运算符在 ES6 中已经引入不过 ES6 中只针对数组在 ES9 中为对象提供了像数组一样的 Rest 参数和扩展运算符*/function connect({ host, port, ...user }) {console.log(host);console.log(port);console.log(user)}connect({ host: 127.0.0.1, port: 3306, username: root, password: root });const skillOne {q: qq};const skillTwo {w: ww};const skillThree {e: ee}const mengseng { ...skillOne, ...skillTwo, ...skillThree };console.log(mengseng); //{q: qq, w: ww, e: ee}/script /body/html正则表达式命名捕获组 ES9 允许命名捕获组使用符号『?』,这样获取捕获结果可读性更强 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title正则表达式命名捕获组/title /headbodyscript//声明一个字符串let str a hrefhttp://vmcode.comvmcode/a;//提取 url 与 标签文本const reg /a href(.*)(.*)\/a/;//执行const result reg.exec(str); /* 0: a hrefhttp://vmcode.comvmcode/a1: http://vmcode.com2: vmcodegroups: undefinedindex: 0input: a hrefhttp://vmcode.comvmcode/alength: 3*/console.log(result);console.log(result[1]);console.log(result[2]);/scriptscript//声明一个字符串let str1 a hrefhttp://vmcode.comvmcode/a;//提取 url 与 标签文本const reg1 /a href(?url.*)(?text.*)\/a/;const result1reg1.exec(str1);console.log(result1) /* [a hrefhttp://vmcode.comvmcode/a, http://vmcode.com, vmcode, index: 0, input: a hrefhttp://vmcode.comvmcode/a, groups: {…}]0: a hrefhttp://vmcode.comvmcode/a1: http://vmcode.com2: vmcodegroups: {url: http://vmcode.com, text: vmcode}index: 0input: a hrefhttp://vmcode.comvmcode/alength: 3*/console.log(result1.groups.url); //http://vmcode.com/script /body/html正则表达式反向断言 ES9 支持反向断言通过对匹配结果前面的内容进行判断对匹配进行筛选。 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title正则表达式反向断言/title /headbodyscript//声明一个字符串let str JS123456你知道吗555啦啦啦;//正向断言const reg/\d(?啦)/;const resultreg.exec(str);console.log(result); /*0: 555groups: undefinedindex: 12input: JS123456你知道吗555啦啦啦length: 1__proto__: Array(0)*///反向断言const reg1/(?吗)\d/;const result1reg1.exec(str);console.log(result1); /*0: 555groups: undefinedindex: 12input: JS123456你知道吗555啦啦啦length: 1__proto__: Array(0)*//script /body/html正则表达式 dotAll 模式 正则表达式中点.匹配除回车外的任何单字符标记『s』改变这种行为允许行终止符出现 !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title正则表达式-dotAll模式/title /headbodyscript//dot . 元字符 除换号符以外的任意单个字符let str ullia肖生克的救赎/ap上映日期1994-09-10/p/lilia阿甘正传/ap上映日期1994-07-06/p/li/ul;const reg /li\sa(.*?)\/a\sp(.*?)\/p/;const result reg.exec(str);console.log(result); /* 0: li↵ a肖生克的救赎/a1: 肖生克的救赎groups: undefinedindex: 25input: ul↵ li↵ a肖生克的救赎/a↵ p上映日期1994-09-10/p↵ /li↵ li↵ a阿甘正传/a↵ p上映日期1994-07-06/p↵ /li↵ /ullength: 2__proto__: Array(0)*/const reg1 /li.*?a(.*?)\/a.*?p(.*?)\/p/s;const result1 reg1.exec(str);console.log(result1); /*0: li↵ a肖生克的救赎/a↵ p上映日期1994-09-10/p1: 肖生克的救赎2: 上映日期1994-09-10groups: undefinedindex: 25input: ul↵ li↵ a肖生克的救赎/a↵ p上映日期1994-09-10/p↵ /li↵ li↵ a阿甘正传/a↵ p上映日期1994-07-06/p↵ /li↵ /ullength: 3__proto__: Array(0*/const reg2 /li.*?a(.*?)\/a.*?p(.*?)\/p/gs;let result2;let data [];while (result2 reg2.exec(str)) {//console.log(result2[1]);data.push({ title: result2[1], time: result2[2] });}console.log(data); /*0: {title: 肖生克的救赎, time: 上映日期1994-09-10}1: {title: 阿甘正传, time: 上映日期1994-07-06}length: 2*//script /body/htmlECMAScript系列ES10新特性 Object.fromEntries !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titleObject.fromEntries/title /headbodyscript//二维数组const result Object.fromEntries([[name, vmcode],[tags, web,java]])console.log(result); //{name: vmcode, tags: web,java}//Mapconst m new Map();m.set(name,vmcode);const result1 Object.fromEntries(m);console.log(result1); //{name: vmcode}const arrObject.entries({ name:vmcode});console.log(arr); /*[[name,vmcode]]*//script /body/htmltrimStart 和 trimEnd //trimlet str i can ;console.log(str);console.log(str.trimStart(str));console.log(str.trimEnd(str));Array.prototype.flat 与 flatMap !DOCTYPE html html langenheadmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titleflat 与 flatMap/title /headbodyscript//flat 平//将多维数组转化为低位数组const arr [1, 2, 3, [4, 5]];console.log(arr.flat()); //[1,2,3,4,5]const arr1 [1, 2, [3, 4, [5, 6]]];console.log(arr1.flat()); //[1,2,3,4,[5,6]]//参数为深度 是一个数子console.log(arr.flat(2));//[1,2,3,4,5,6]//flatMapconst arr2 [1, 2, 3, 4];const result1 arr2.map(item item * 10);console.log(result1); // [10, 20, 30, 40]const result2 arr2.flatMap(item [item * 10]);console.log(result2); // [10, 20, 30, 40]/script /body/htmlSymbol.prototype.description //创建 Symbollet s Symbol(vmcode);console.log(s.description); //vmcodeECMAScript系列ES11新特性 String.prototype.matchAll script//用来得到正则批量结果let str ullia肖生克的救赎/ap上映日期1994-09-10/p/lilia阿甘正传/ap上映日期1994-07-06/p/li/ul;//声明正则const reg/li.*?a(.*?)\/a.*?p(.*?)\/p/sg//得到结果const resultstr.matchAll(reg);console.log(result); //RegExpStringIterator {}// for(let v of result){// console.log(v[1]);// }const arr[...result];console.log(arr); /* (2) [Array(3), Array(3)]0: (3) [li↵ a肖生克的救赎/a↵ p上映日期1994-09-10/p, 肖生克的救赎, 上映日期1994-09-10, index: 25, input: ul↵ li↵ …↵ /li↵ /ul, groups: undefined]1: (3) [li↵ a阿甘正传/a↵ p上映日期1994-07-06/p, 阿甘正传, 上映日期1994-07-06, index: 159, input: ul↵ li↵ …↵ /li↵ /ul, groups: undefined]length: 2__proto__: Array(0)*/类的私有属性 scriptclass Person {//公有属性name;//私有属性#age;#weight;constructor(name, age, weight) {this.name name;this.#age age;this.#weight weight;}intro(){console.log(this.#age);console.log(this.name);}}//实例化const girl new Person(vmcode, 18, 250);console.log(girl); /*{name: vmcode#age: 18 #weight: 25}*/console.log(girl.name); //vmcode//console.log(girl.#age); // Private field #age must be declared in an enclosing classgirl.intro();Promise.allSettled //适用于批量Promise //声明两个promise对象const p1new Promise((resolve,reject){setTimeout((){resolve(product data -1)},1000)})const p2new Promise((resolve,reject){setTimeout((){reject(product data -2)},1000)})//调用 allsettled 方法 始终是个成功const resultPromise.allSettled([p1,p2]);console.log(result); /* __proto__: Promise[[PromiseState]]: fulfilled[[PromiseResult]]: Array(2)0: {status: fulfilled, value: product data -1}1: {status: fulfilled, value: product data -2}length: 2__proto__: Array(0)*///要么都成功要么就显示失败const resPromise.all([p1,p2]);console.log(res); /*__proto__: Promise[[PromiseResult]]: product data -2[[PromiseState]]: rejected*/ 可选链操作符 //?.function mian(config){//const dbHostconfig.db.host;const dbHostconfig?.db?.host;console.log(dbHost);}//mian({db:{host:127.0.0.1,username:root},cache:{host:127.0.0.1,username:admin}})mian();动态 import 导入 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title动态import/title /head body button idbtnbtn/buttonscript src./js/app.js typemodule/script /body /htmlApp.js //import * as m1from ./hello.jsconst btndocument.getElementById(btn);btn.onclickfunction(){import(./hello.js).then(moudle{console.log(moudle);moudle.hello();}) }Hello.js export function hello(){console.log(hello); }BitInt let n512n;console.log(n,typeof(n)) //512n bigint//函数let n1123;console.log(BigInt(n1)); //123n//console.log(BitInt(1.2)); // BitInt is not defined//大数值运算let maxNumber.MAX_SAFE_INTEGER;console.log(max);//9007199254740991console.log(max1); //9007199254740992console.log(max2); //9007199254740992console.log(BigInt(max));//9007199254740991nconsole.log(BigInt(max)BigInt(1)); //9007199254740992nconsole.log(BigInt(max)BigInt(2)); //9007199254740993nglobalThis 对象 html下 !DOCTYPE html html langen headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titleglobalThis/title /head bodyscriptconsole.log(globalThis); //window/script /body /htmlnodejs下 console.log(globalThis); //global
http://www.dnsts.com.cn/news/77049.html

相关文章:

  • 网站建设项目方案企业网站建设技
  • 网站建设价格费用如何做网站流程图
  • 广东省建设工程金匠奖公布网站常见的手机网站
  • 便宜的自助建站免费咨询法律律师在线12348
  • 长沙网站开发流程上海网站建设招聘
  • 毕业设计做网站难吗搭建网络平台的计划书
  • 关于电商网站建设的论文建筑专业网站有哪些
  • 网站显示数据一个微信可以做两个网站支付
  • 南通网站建设系统电话余杭专业的网页制作官网
  • 如何创建刷单网站提供广州网站建设
  • 图书类网站建设策划书视频网站开发公司有哪些公司
  • 帮别人设计做关于图的网站net的电商网站建设
  • 知名的seo快速排名多少钱seo网站推广电话
  • 长垣县建站塔山双喜wordpress多站点demo
  • 织里网站建设婚纱摄影图片
  • 德清县住房和城乡建设局网站商业网站建设案例课程下载
  • 自己建购物网站建设工程扣分查询网站
  • 做合法的海外购网站需要什么手续网站建设需要方案
  • 有关电商网站开发的实习报告每平每屋设计家官网
  • wordpress 便签优化大师的功能有哪些
  • wap网站编辑器网站图片多大合适
  • 做自行车网站应该注意什么做电商在什么网站吗
  • 做外贸最好的网站有哪些巩义网站建设与制作
  • 学校信息化网站建设深圳网站建设价格是多少
  • pc蛋蛋网站开发做301到别人网站
  • 深圳网站建设推进西安商城网站建设
  • 万网域名备案网站筑招建筑人才网长春安全员
  • 济宁网站建设seo模板网站能用吗
  • 站酷设计官方网站做网站源代码
  • 什么网站不能备案wordpress本地环境迁移步骤