1.let
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组的用法</title>
<script>
let log = (text)=>console.log(text);
</script>
</head>
<body>
<pre>
ES5:
array.forEach()
array.map()
返回值修改数组内容,重新整理数据结构
array.filter()
过滤不合适的元素,如果返回结果为true则留下来
array.some()
类似查找,数组中某一个元素符合条件,返回true
array.every()
数组里面的元素都要符合条件,才返回true
array.reduce()
pre,是第一次的结果
array.reduceRight()
从右边开始计算
array.for...of
</pre>
<script type="application/javascript">
let arr1 = ['jack','mary','tom','blush','fen'];
log("forEach普通函数:");
arr1.forEach(function (val,index,arr1) {
console.log(index,val,arr1);
});
log("forEach箭头函数:");
arr1.forEach((val,index,arr1)=>{
console.log(index,val,arr1)
});
log("map映射:");
let news = [
{title:'new1',read:199,hot:false},
{title:'new2',read:299,hot:true},
{title:'new3',read:399,hot:false},
{title:'new4',read:499,hot:true},
{title:'new5',read:599,hot:false},
];
let mapNews = news.map((val,index,news)=>{
console.log(index,val,news[index].read);
let item = {};
item.t = val.title+"map";
item.r = val.read+1;
item.h = false;
return item;
});
log(mapNews);
log("filter过滤:");
let filterNews = news.filter((val,index,news)=>{
console.log(index,val,news[index].read);
return val.hot;
});
log(filterNews);
let arr2 = [1,2,3,4,5,6,7,8,9,10];
let sum = arr2.reduce((pre,cur,index,arr2)=>{
return pre + cur;
});
log("reduce:"+sum);
log("ES6:");
for(let val of news){
console.log(val.title);
}
for(let [key,val] of news.entries()){
console.log(key,val);
}
</script>
</body>
</html>
2.struct
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>解构赋值</title>
</head>
<body>
<div>
<pre>
解构赋值
let [a,b,c] = [1,2,3];
console.log(a,b,c);
let [a,[b,c]] = [1,[2,3]];
console.log(a,b,c);
let json = {
name:'jack',
age:28,
job:'work'
};
let {name,age,job} = json;
let {name:nick,age:num,job:work} = json;
console.log(nick,num,work);
</pre>
</div>
<script type="application/javascript">
console.log("解构赋值:");
{
let [a,b,c] = [1,2,3];
console.log(a,b,c);
}
console.log("解析结构:")
{
let [a,[b,c]] = [1,[2,3]];
console.log(a,b,c);
}
console.log("给变量默认值:");
{
let [a,b,c = 'default data'] = [1,2];
console.log(a,b,c);
let [d,e,f = 'default data'] = [1,2,undefined];
console.log(d,e,f);
let [g,h,i = 'default data'] = [1,2,null];
console.log(g,h,i);
let [j,k,l = 'default data'] = [1,2,''];
console.log(j,k,l);
}
console.log("解析Json字符串:");
{
let json = {
name:'jack',
age:28,
job:'work'
};
let {name,age,job} = json;
console.log(name,age,job);
console.log("给解析后的属性取别名:");
let {name:nick,age:num,job:work} = json;
console.log(nick,num,work);
}
console.log("获取位置信息:");
{
function getPos() {
return {
left:10,
top:12
};
}
let {top,left} = getPos();
console.log(left,top);
}
console.log("参数解构:");
{
function show1({a,b = '默认值'}) {
console.log(a,b);
}
show1({a:'小明',b:'小花'});
function show2({a = '默认值a',b = '默认值b'}) {
console.log(a,b);
}
show2({});
function show3({a,b = '默认值b'}={a:'a'}) {
console.log(a,b);
}
show3();
}
</script>
</body>
</html>
3.array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组的用法</title>
<script>
let log = (text)=>console.log(text);
</script>
</head>
<body>
<pre>
ES5:
array.forEach()
array.map()
返回值修改数组内容,重新整理数据结构
array.filter()
过滤不合适的元素,如果返回结果为true则留下来
array.some()
类似查找,数组中某一个元素符合条件,返回true
array.every()
数组里面的元素都要符合条件,才返回true
array.reduce()
pre,是第一次的结果
array.reduceRight()
从右边开始计算
array.for...of
</pre>
<script type="application/javascript">
let arr1 = ['jack','mary','tom','blush','fen'];
log("forEach普通函数:");
arr1.forEach(function (val,index,arr1) {
console.log(index,val,arr1);
});
log("forEach箭头函数:");
arr1.forEach((val,index,arr1)=>{
console.log(index,val,arr1)
});
log("map映射:");
let news = [
{title:'new1',read:199,hot:false},
{title:'new2',read:299,hot:true},
{title:'new3',read:399,hot:false},
{title:'new4',read:499,hot:true},
{title:'new5',read:599,hot:false},
];
let mapNews = news.map((val,index,news)=>{
console.log(index,val,news[index].read);
let item = {};
item.t = val.title+"map";
item.r = val.read+1;
item.h = false;
return item;
});
log(mapNews);
log("filter过滤:");
let filterNews = news.filter((val,index,news)=>{
console.log(index,val,news[index].read);
return val.hot;
});
log(filterNews);
let arr2 = [1,2,3,4,5,6,7,8,9,10];
let sum = arr2.reduce((pre,cur,index,arr2)=>{
return pre + cur;
});
log("reduce:"+sum);
log("ES6:");
for(let val of news){
console.log(val.title);
}
for(let [key,val] of news.entries()){
console.log(key,val);
}
</script>
</body>
</html>
4.string
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>String字符串</title>
<script>
function log(text) {
console.log(text);
}
</script>
</head>
<body>
<pre>
字符串模板
可以随意换行
let name = "Jack";
let age = 22;
let hobby = "football";
let text = `Hi,大家好,我叫${name},年龄:${age},我爱好:${age}`;
log(text);
字符串查找
includes(str)
字符串匹配
startWith()
endsWith()
字符串重复
repeat(count)
</pre>
<ul id="news">
</ul>
<script type="application/javascript">
let name = "Jack";
let age = 22;
let hobby = "football";
let text = "Hi,大家好,我叫"+name+",年龄:"+age+",我爱好:"+hobby;
log(text);
log("字符串模板输出:");
{
let text = `Hi,大家好,我叫${name},年龄:${age},我爱好:${age}`;
log(text);
}
let data = [
{
title:"上海金秋多雨1",
read:199
},
{
title:"上海金秋多雨2",
read:299
},
{
title:"上海金秋多雨3",
read:399
},
{
title:"上海金秋多雨4",
read:499
}
];
window.onload = function () {
let news = document.querySelector('#news');
for(let i = 0;i<data.length;i++){
let oLi = document.createElement('li');
oLi.innerHTML = `<span>${data[i].title}</span>
<span>阅读人数:${data[i].read}</span>
<a href="#">详情</a>`;
news.appendChild(oLi);
}
}
let str = "Hi,I am apple,How are you,Are you ok?";
log(str);
log("字符串查找[you]:"+str.includes('you'));
let patten = 'apple you like';
log("字符串匹配:");
log(patten.startsWith('apple'));
log(patten.endsWith('like'));
log(patten.repeat(99))
</script>
</body>
</html>
5.func
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ES6函数</title>
<script>
function log(text) {
console.log(text);
}
</script>
</head>
<body>
<pre>
函数改变
1.带默认值
2.解构赋值
3.函数参数默认是已经定义的,并不能再次使用let,const重新函数体内定义
</pre>
<script type="application/javascript">
log("带默认值的:");
function f1(a='Hello',b='Jack ') {
console.log(a+","+b);
}
f1();
f1('Hi');
f1(false,"Mark");
f1(undefined,'Tony');
log("解构赋值:");
function f2({a='999a',b='999b'}={}) {
console.log(a+","+b);
}
f2();
f2({});
f2({a:1,b:2});
log("延展操作数:");
function f3(...a) {
log(a.sort());
}
f3();
f3(5,2,1,4,3);
f3(...[5,2,1,4,3]);
function f4(a,b,...c) {
console.log(a,b);
console.log(c[0],c[1],c[2]);
}
f4(1,2,3,4,5);
log("数组的复制:");
let arr1 = ['a','b','c','d','e'];
let arr2 = [...arr1];
console.log(arr1,arr2);
log("箭头函数:");
let f5 = () => {
log("begin-->");
console.log(3);
console.log(4);
log("end-->");
};
f5();
let f6 = (x,y)=>x+y;
log(f6(2,3));
let json = {
id:1,
name:'jack',
show:function () {
console.log("json函数 Id = "+this.id);
setTimeout(() => {
console.log("延迟2秒后打印:Name = "+this.name);
},2000);
}
};
json.show();
function f7() {
this.name = 'f7';
}
log(new f7().name);
</script>
</body>
</html>
6.gene
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Generator</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<pre>
Symbol()
返回值是一个唯一值,可以做key用
generator生成器函数
解决异步
for...of...
let [a,b,c] = gen();
...
console.log(...gen());
console.log(Arrays.from(gen()));
gen.next(参数).value = yield后面计算的结果,其中参数为本次yield返回值
</pre>
<script>
let sym = Symbol('aaa');
console.log(sym);
function * show() {
console.log("生成器函数....");
yield 'welcome';
yield 'to';
yield 'home';
return 'jack';
}
let g1 = show();
let g2 = show();
console.log('手动调用');
console.log(g1.next());
console.log(g1.next());
console.log(g1.next());
console.log(g1.next());
console.log('循环调用');
for (let val of g2){
console.log(val);
}
console.log('解构赋值');
let [a,b,c] = show();
console.log(a,b,c);
console.log(...show());
//https://api.github.com/users/tomtt
function* getUserInfo() {
let userName = yield 'tomtt';
yield axios.get(`https://api.github.com/users/${userName}`);
}
console.log('模拟请求');
let gg = getUserInfo();
let username = gg.next().value;
console.log(username);
// console.log(gg.next());
gg.next(username).value.then(res=>{
console.log(res.data);
})
</script>
</body>
</html>
7.asyn
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Async</title>
</head>
<body>
<pre>
await只能放在async函数里面
</pre>
<script>
console.log("Promise写法");
function * gen() {
yield readFile('data/a.txt');
yield readFile('data/b.txt');
yield readFile('data/c.txt');
}
let g1 = gen();
g1.next().value.then(res=>{
console.log(res);
return g1.next().value;
}).then(res=>{
console.log(res);
return g1.next().value;
}).then(res=>{
console.log(res);
})
console.log("async");
async function fff() {
let data1 = await readFile('data/a.txt');
console.log(data1);
let data2 = await readFile('data/b.txt');
console.log(data2);
let data3 = await readFile('data/c.txt');
console.log(data3);
}
</script>
</body>
</html>
8.set
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Set</title>
</head>
<body>
<pre>
Set存储数组
let arr = new Set(['','','',''])
WeakSet存储json
let arr = new WeakSet();
Map
</pre>
<script>
let setArr = new Set(['a','b','c','a']);
setArr.add('e');
setArr.delete('a');
console.log(setArr);
let map = new Map();
map.set(1,'jack1');
map.set(2,'jack2');
map.set(3,'jack3');
console.log(map);
</script>
</body>
</html>