好久没更新了

This commit is contained in:
asd
2021-03-23 10:58:10 +08:00
parent eca64f86c0
commit 9bf8e8d020
123 changed files with 24337 additions and 214 deletions

9
es6/a.js Normal file
View File

@@ -0,0 +1,9 @@
const sqrt = Math.sqrt;
function square(x) {
return x * x;
}
function diag(x, y) {
return sqrt(square(x) + square(y));
}
export {sqrt, square, diag}

78
es6/async.html Normal file
View File

@@ -0,0 +1,78 @@
<!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>
<script>
// const fs = require('fs');
// const readFile = function (fileName) {
// return new Promise(function (resolve, reject) {
// fs.readFile(fileName, function (error, data) {
// if (error) return reject(error);
// resolve(data);
// });
// });
// };
// const gen = function* () {
// const f1 = yield readFile('/etc/fstab');
// const f2 = yield readFile('/etc/shells');
// console.log(f1.toString());
// console.log(f2.toString());
// };
// // *是 Generator 的声明
// const gen2 = async function () {
// const f1 = await readFile("file1")
// const f2 = await readFile("file2")
// console.log(f1.toString())
// console.log(f2.toString())
// }
// // async 声明这个函数是 异步函数 只有在被async修饰的函数中才能使用await 关键字
// // await 等待后面的操作完成后才继续执行
// async function getStockPriceByName(name) {
// const symbol = await getStockSymbol(name);
// const stockPrice = await getStockPrice(symbol);
// return stockPrice;
// }
// getStockPriceByName('goog').then(function (result) {
// console.log(result);
// });
async function testAsync() {
return "hello async";
}
async function t1(){
var data = testAsync();
console.log(data)
}
async function t2(){
var data = await testAsync();
console.log(data)
}
t1() //没加await修饰的时候 返回的是promise对象
t2() // 加了await的时候 返回的是promise对象中的value的值 即 hello async!
// var list =await getlist("url");
// var uinfo =await getinfo("url");
// 。。。
</script>
</head>
<body>
</body>
</html>

68
es6/b.js Normal file
View File

@@ -0,0 +1,68 @@
import { square, diag } from './a.js';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5
// promise 构造函数接受一个函数作为参数该函数的两个参数分别是resolve,reject
// 成功时调用resolve函数失败时调用reject函数
// const promise = new Promise(function(resolve, reject) {
// // ... some code
// if (/* 异步操作成功 */){
// resolve(value);
// } else {
// reject(error);
// }
// });
// promise.then(function(value) {
// // success
// }, function(error) {
// // failure
// })
let promise = new Promise(function(resolve, reject) {
console.log('Promise');
resolve();
});
promise.then(function() {
console.log('resolved.');
});
console.log('Hi!');
const getJSON = function(url) {
const promise = new Promise(function(resolve, reject){
const handler = function() {
if (this.readyState !== 4) {
return;
}
if (this.status === 200) {
resolve(this.response);
} else {
reject(new Error(this.statusText));
}
};
const client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();
});
return promise;
};
getJSON("/posts.json").then(function(json) {
console.log('Contents: ' + json);
}, function(error) {
console.error('出错了', error);
});

125
es6/bianliang.html Normal file
View File

@@ -0,0 +1,125 @@
<!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>
<script>
{
let a = 1;
var b = 2;
a = 9;
let a = "llp"
}
// console.log(a)
// console.log(b)
for (var i = 0; i < 5; i++) {
console.log(i)
}
console.log(i) //5
for (let m = 0; m < 5; m++) {
console.log(m, "llk")
}
// console.log(m)
var a = [];
for (var i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);
};
}
a[6](); // 10
var a = [];
for (let i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);
};
}
a[6](); // 10
const c = 10
c = 9;
console.log(d)
var d = 7
// let aa = 1;
// let bb = 2;
// let cc = 3;
// |
let [aa, bb, cc] = ["a", "b", "c"] //解构赋值
let [foo, [[bar], baz]] = [1, [[2], 3]];
// foo 1
// bar 2
// baz 3
let [, , third] = ["foo", "bar", "baz"];
third // "baz"
let [x, , y] = [1, 2, 3];
x //1
y //3
let [head, ...tail] = [1, 2, 3, 4];
head // 1
tail // [2,3,4]
let [x, y, ...z] = ['a'];
x // a
y // undefined
z // []
// ...先生成一个数组,把后面所有的元素都塞进去
let [a, [b], d] = [1, [2, 3], 4];
a // 1
b // 2
d // 4
// 不完全解构的时候 左边的变量名只能匹配到第一个值作为他的变量值
// 报错
// let [foo] = 1;
// let [foo] = false;
// let [foo] = NaN;
// let [foo] = undefined;
// let [foo] = null;
// let [foo] = {};
// 如果等号的右边不是数组或者严格地说不是可遍历的结构参见《Iterator》一章那么将会报错。
</script>
</head>
<body>
结论 用Let声明的变量只在当前代码块中能够访问
用var声明的变量在代码块内部外部都能访问
在同一个代码块中let 不能重复定义
let 不存在var的变量提升
<p>
const 声明的是常量, 定义完成以后值不能被修改 所以在定义的时候需要给他赋初始值
</p>
</body>
</html>

89
es6/promise.html Normal file
View File

@@ -0,0 +1,89 @@
<!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>
<!-- <script type="module" src="./b.js"></script> -->
<script>
// 定义一个 promise 对象
// var promise = new Promise(function (resolve, reject) {
// // resolve 成功状态下调用的函数
// // reject 失败状态下调用的函数
// if (成功) {
// resolve()
// } else {
// reject()
// }
// })
// console.log(promise)
// // 使用promise 对象
// promise.then(function (res) {
// console.log(res) //成功返回的值
// }).catch(function (err) {
// console.log(err) //失败返回的值
// })
// function timeout(ms) {
// return new Promise((resolve, reject) => {
// setTimeout(resolve, ms, 'done');
// });
// }
// timeout(100).then((value) => {
// console.log(value);
// });
const getJSON = function (url) {
const promise = new Promise(function (resolve, reject) {
const handler = function () {
if (this.readyState !== 4) {
return;
}
if (this.status === 200) {
resolve(this.response);
} else {
reject(new Error(this.statusText));
}
};
const client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
// client.setRequestHeader("Accept", "application/json");
client.send();
});
return promise;
};
getJSON("/posts.json").then(function (json) {
console.log('Contents: ' + json);
}).catch(function(err){
console.log(err)
});
async function s(){
var list =await getJSON("url")
}
</script>
</head>
<body>
</body>
</html>

96
es6/setmap.html Normal file
View File

@@ -0,0 +1,96 @@
<!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>
<script>
var arr = [1, 1, 2, 2, 3, 3, 3]
// console.log(arr)
let set = new Set(arr)
// console.log(set)
set[3] = 2
// console.log(set)
// set 作为数据结构,里面的成员都是唯一的
let strarr = new Set(["s", "l", "S", "L"])
// console.log(strarr) //[s,l,S,L]
strarr.delete("s")
// console.log(strarr, "new")
// console.log(strarr.has("L"))
for (let i of strarr.keys()) {
// console.log(i)
} //for in 会失败
for (let i of strarr.values()) {
// console.log(i)
}
//set实例的键和值都是一样的 所以用keys()/values()的时候 遍历得到的结果是一样的
// entries() 返回键+值
for (let i of strarr.entries()) {
// console.log(i)
}
//Array [ "l", "l" ]
// Array["S", "S"]
// Array["L", "L"]
// 向 Set 加入值的时候不会发生类型转换所以5和"5"是两个不同的值。相同字母的大小写也是不同的值
//set 属性
// set.prototype.constructor 构造函数
// set.prototype.size 返回set实例的值的个数
// set 方法
// set.prototype.add(要添加的值) 向set实例中添加一个元素
// set.prototype.delete(删除的值) 删除set实例中的一个元素
// set.prototype.clear() 清空
// set.prototype.has(要判断的值) 判断是否在set实例中 返回的是布尔值
// Array.from(set的实例) 将set实例转成数组
// 数组去重
// function dedupe(array) {
// return Array.from(new Set(array));
// }
// dedupe([1, 1, 2, 3]) // [1, 2, 3]
// map
const map = new Map([
['name', '张三'],
['title', 'Author']
]);
console.log(map)
const m = new Map();
const o = { p: 'Hello World' };
m.set(o,'content') // m.set(key,value) map实例中能用对象来当键
//json 方法:
// json.stringfy() 对象转成json
// json.parse() json转对象
console.log(m.get(o))
</script>
</head>
<body>
</body>
</html>