基本写法
<script>
function func1() {
console.log('func1');
}
const func2 = function () {
console.log('func2');
};
const func3 = () => {
console.log('func3');
};
func1();
func2();
func3();
</script>
传递参数
<script>
const func2 = a => {}
const func2 = () => {}
const func2 = (a, b) => {}
</script>
返回值
<script>
const func1 = () => {
return 123;
};
const func2 = (a) => a + 1
console.log(func2(2));
</script>
返回对象
<script>
const func1 = () => 123;
const func2 = () => 'abc';
const func3 = () => true;
const func4 = () => [1, 2, 34];
const func5 = () =>( { username: '悟空' });
</script>
默认值
<script>
const func1 = (msg = '大家好') => {
console.log(msg);
};
func1("你好");
const getLength = (arr = []) => console.log(arr.length);
const list=['a'];
getLength(list);
getLength()
</script>
解构
数组的解构
const arr = ['a', 'b', 'c'];
以前的获取方法
const str1 = arr[0];
const str2 = arr[1];
console.log(str1,str2);
数组解构 关注 顺序
const [str1, str2] = arr;
console.log(str1,str2);
交换变量 解构
let a = 100;
以前的写法
let b = 200;
let c = a;
a = b;
b = c;
console.log(a,b);
交换变量 解构
let a = 100;
let b = 200;
[b, a] = [a, b];
console.log(a, b);
对象解构
const obj = { username: '悟空', skill: '72' };
以前写法
const username=obj.username;
const skill=obj.skill;
对象解构写法
const { username, skill } = obj;
console.log(username);
console.log(skill);
对象的简写
<script>
const userame = '悟空'
const skill = '72'
const age = 500
const obj = {
username: username,
skill: skill,
};
const obj1 = {
userame,
skill,
age,
}
console.log(obj);
const obj2 = {
userame,
say() {
console.log('这是一个方法');
}
}
obj.say()
</script>
剩余运算符
<script>
const arr = ['a', 'b', 'c', 'd']
const [let1, ...list] = arr
console.log(list);
const obj = { a: 1, b: 2, c: 3, d: 4 };
const { a, b, ...objs } = obj;
console.log(objs,);
calc(1);
calc(1,2);
calc(1,2,3);
calc();
function calc(...params) {
console.log(params);
}
</script>
复制引用类型,剩余运算符
<script>
const obj = { username: '悟空', age: 500 };
const newObj1 = {...obj};
newObj1.username = '八戒';
console.log(obj);
console.log(newObj1);
const list = ['a', 'b']
const newList = [...list]
newList.push('c')
console.log(list);
console.log(newList);
</script>
数组方法
下列数组方法总结
map() 处理数组的'每个元素',并返回处理后的数组
every() 检测数值元素的'每个元素'是否都符合条件
some() 检测数组'是否有符合指定条件'
filter() 检测数值元素,并返回'符合条件'所有元素的数组
find() 用来找数组中满足条件的'一个元素'
findIndex() 符合条件的元素的'下标'
indexOf() 搜索数组中的元素,并返回它所在的位置
map()
通过指定函数处理数组的每个元素,并返回处理后的数组。
const arr = ['a', 'b', 'c']
const newArr = arr.map(function (value,index) {
return `<li>${index}-${value}</li>`
})
console.log(newArr);
const arr = ['a', 'b', 'c']
const newArr = arr.map((value) => `<li>${value}</li>`)
console.log(newArr);
every()
检测数值元素的'每个元素'是否都符合条件。
<script>
const names = ['黄圣飞', '梁子聪', '王锦双', '韦嘉敏', '雅琴']
const result = names.every((value) => value.length > 2)
console.log(result);
</script>
some()
检测数组元素中是否有元素符合指定条件
const arr = ['阴性','阴性','阴性','阴性','阳性','阴性','阴性','阴性']
const result = arr.some((value) => value === '阳性')
if(result) {
console.log('要完蛋了');
}else {
console.log('你好帅');
}
console.log(result);
filter()
检测数值元素,并返回'符合条件'所有元素的数组。
const arr = [1, 2, 5, 7, 8, 9]
基本写法
箭头函数写法
const newArr = arr.filter((value) => value > 3)
console.log(newArr);
find
<script>
find 用来找数组中满足条件的一个元素
找到了之后 不会再继续往下遍历
代码中 找到了 就需要返回true
const arr = [
{ username: '悟空', height: 70 },
{ username: '八戒', height: 60 },
{ username: '龙马', height: 30 },
{ username: '龙马', height: 30 },
];
const obj = arr.find((value) => value.height === 60);
console.log(obj);
</script>
findIndex
<script>
const arr = [
{ username: '悟空', height: 70 },
{ username: '八戒', height: 60 },
{ username: '龙马', height: 30 },
];
const index = arr.findIndex((value) => value.height === 660);
console.log(index);
</script>
includes()
<script>
const arr = ['a', 'b', 'c', 'd'];
const result = arr.includes('e');
console.log(result);
</script>
indexOf
<script>
const arr = ['a', 'b', 'c', 'd'];
const index = arr.indexOf('h');
console.log(index);
</script>
join
<script>
const arr = ['a', 'b', 'c'].map((value) => `<li>${value}</li>`);
const result = arr.join('');
console.log(result);
</script>
Set 对象转数组
/永远不会有重复元素的对象
const set = new Set([1, 5, 3, 4]);
set.add(5);
set.add(5);
console.log(set);
const arr = [...set];
console.log(arr);
改变this的指向
以下方法总结
'相同点'
都可以改变函数内部的this指向
'区别点'
1 call 和 apply 会调用函数,并且改变函数内部this指向
2 call 和 apply 传递的参数不一样,call传递参数 ,参数1,参数2,...形式 apply必须数组形式 [参数]
3 bind 不会调用函数,可以改变内部this指向
'主要应用场景'
1 call 经常做继承
2 apply 经常跟数组有关系,比如借助于数学对象实现数组最大值最小值
3 bind 不调用函数,但是还想改变this指向,比如改变定时器内部的this指向
'全局函数 this 指向 window'
'箭头函数没有 this'
call
function Person(name, color, height, weigth) {
this.name = name
this.color = color
this.height = height
this.weigth = weigth
}
function Son(name, color, height, weigth) {
Person.call(this, name, color, height, weigth)
}
const son = new Son('悟空', '黄色', 120, 240)
apply
let arr = [1, 35, 57, 78, 99];
let max = Math.max.apply(Math, arr)
let min = Math.min.apply(Math, arr)
console.log(max,min);
bind
<script>
const obj = {
name: '三毛'
}
function fn(a, b) {
console.log(this)
console.log(a + b)
}
const f = fn.bind(obj, 1, 2)
f(2,3)
</script>
class
<script>
' constructor()接收参数的,如果儿子也用了constructor 就一定要用super()把继承过来的属性装里面'
class Preson {
constructor(name, color) {
this.name = name
this.color = color
}
}
class YellowPerson extends Preson {
constructor(name, color, height, weight) {
super(name, color)
this.height = height
this.weight = weight
}
}
const yp = new YellowPerson('三毛', '黄毛', 123, 234)
console.log(yp);
</script>
检测数据类型
<script>
class Person {}
class SuperPerson{}
const p1 = new Person();
const s1 = new SuperPerson();
console.log(p1 instanceof Person);
console.log(p1 instanceof SuperPerson);
</script>