(发现上机实习课最好的打开方式就是不做课设)
这次的知识点挺多的,按照时间顺序上传下。
1-ES7部分新特性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const a1=[1,2,'UMP45','HK416']
console.log(a1.includes('HK416'));
//判断参数是否存在于其中,返回布尔值
//**求幂运算符
</script>
</body>
</html>
2-ES8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//大的来了,async+await
//async函数 (返回结果一定为promise对象<无论你怎么写 return >)
async function fn1() {
//返回结果不是promise,则结果是成功的promise,值为实际return 的对象
//使用 throw new err() 抛出错误,返回一个失败的promise
//返回结果为promise对象,则看return 的promise结果是什么
return new Promise((res, rej) => {
res('Success!')
})
}
const r1 = fn1();
console.log(r1);//像一个语法糖
r1.then((value) => {
console.log(value);
}, (reason) => {
console.log(reason);
})
</script>
</body>
</html>
es8-await
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const p = new Promise((res, rej) => {
res('im robot')
rej('失败了')
})
// await 必须放在 async 里面!!!
// await右面是一个promise对象
// await 接受 Promise 对象成功的值(resolve)!!!!!
// 完整形式应该使用try...catch进行捕获错误❌
async function fn() {
try {
let result = await p;
console.log(result);
}catch(e){
console.log(e);
}
}
fn()
</script>
</body>
</html>
案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//使用 Promise then 实现 Ajax接受返回参数
//发送Ajax,返回Promise对象
function sendAjax(url) {
//0->返回promise对象
return new Promise((res, rej) => {
//1-创建对象
const x = new XMLHttpRequest();
//2-初始化
x.open('GET', url);
//3-发送
x.send();
//4-事件绑定
x.onreadystatechange = function () {
if (x.readyState === 4)
if (x.status >= 200 && x.status < 300) {
//成功时
res(x.response);
}else{
rej(x.status)
}
}
})
}
// //promise + then 核心部分
// const result = sendAjax('https://api.apiopen.top/getJoke').then((value)=>{
// console.log(value);
// })
// // console.log(result);
//使用async+await实现
async function doit(){
let result = await sendAjax('https://api.apiopen.top/getJoke');
console.log(result);
}
doit()
</script>
</body>
</html>
async + await js文件
const fs = require('fs')
//读取
function Readn1() {
return new Promise((res, rej) => {
fs.readFile('./note.txt', (err, data) => {
if (err) rej(err)
res(data)
})
})
}
function Readn2(){
return new Promise((res,rej)=>{
fs.readFile('./note2.txt',(err,data)=>{
if(err) rej(err)
res(data)
})
})
}
function Readn3(){
return new Promise((res,rej)=>{
fs.readFile('./note3.txt',(err,data)=>{
if(err) rej(err)
res(data)
})
})
}//这三个函数的返回结果都是promise对象
async function core(){
try{//完整写法,需要捕获错误
let n1 = await Readn1();//await返回的结果是promise成功的值
let n2 = await Readn2();
let n3 = await Readn3();
//结果都获取过来了
console.log((n1+n2+n3).toString());
}catch(e){
console.log(e);//一旦出现错误便会捕获
}
}
core();
//这样书写代码,会和同步调用非常相似,只是前面多了一个async 或者 await
导入导出
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module">
//注意script 里面有 type='module' 这一段,使其确定为module模块引入
import * as m1 from './m1.js'//导入m1中所有暴露的值和函数到m1变量中
console.log(m1);
import * as m2 from './m2.js'
console.log(m2);
import * as m3 from'./m3.js'
console.log(m3);//这样导入会和前俩种的结构有所不同
//以上为通用导入方式
//2-解构赋值方式
import {name,kill} from './m1.js'
console.log(name,kill);
//<别名的使用>
import {name as name1,goaway} from './m2.js'
console.log(name1,goaway);
//3-导入以默认方式暴露的一种导入方式
import{default as m33} from './m3.js'
console.log(m33);
//简便形式---只支持默认暴露
import m333 from './m3.js'
console.log(m333);
</script>
</body>
</html>
相应js
export let name='Ellie'
export function kill(){
console.log('Press X to kill');
}
//此方法为分别暴露
//此方法为统一暴露
let name='Elizabeth'
function goaway() {
console.log('im out');
}
export {name,goaway}
//默认暴露
export default{
name:'UMP45',
attack() {
console.log('open fire');
}
}
//这样导出会和前俩中有所不同
入口文件
js->
//入口文件
import * as m1 from './m1.js'
import * as m3 from './m3.js'
console.log(m1,m3);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./app.js" type="module"></script>
<!-- 同样注意script 里面有 type='module' 这一段,使其确定为module模块引入 -->
这个只是理想状态下的引入(可能会有兼容性等问题 fxxk IE)
实际上一般采用babel对此进行处理
而babel是一个JavaScript编译器,可以使新的js语法编译为旧版本的
</body>
</html>
babel的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 1-安装工具 babel-cli babel-preset-env browserify(webpack在项目中)-->
<!-- 2-通过babel进行转换 -->
<!-- 3-打包处理 npx browserify dist/js/app.js -o dist/bundle.js -->
<!-- 只有在打包以后才可以进行引入等操作 -->
<!-- 改动文件后运行上面那条语句就可以做到更新 -->
<script src="./dist/bundle.js"></script>
<!-- 详细安装步骤
1- npm init
2- npm i babel-cli babel-preset-env browserify -D 安装包并使用 -D 开发依赖
3- npx babel ./ -d dist --presets=babel-preset-env
npx命令 (要改动的js) -d (改后存储的位置) --~默认设置
4- npx browserify dist/js/app.js -o dist/bundle.js 进行对目标js打包
-->
</body>
</html>