对象特点-继承
属性继承
function Father(num, color, age) {
this.num = num;
this.color = color;
this.age = age;
}
function Son(num, color, age, name) {
Father.call(this, num, color, age);
this.name = name;
}
const son1 = new Son(11, "red", 22, "小明");
console.log(son1);
方法继承
function Father(num, color, age) {
this.num = num;
this.color = color;
this.age = age;
}
Father.prototype.say = function () {
console.log(this.color + "是什么颜色");
};
Father.prototype.saySomething = function () {
console.log(this.num + 11);
};
function Son(num, color, name, num1) {
Father.call(this, num, color);
this.name = name;
this.num1 = num1;
}
Son.prototype.say = Father.prototype.say;
Son.prototype.saySomething = Father.prototype.saySomething;
const son1 = new Son(11, "red", "小明", 22, 33);
son1.say();
son1.saySomething();
ES6
函数参数默认值
function name(num = "1", age = "222") {
console.log(num, age);
}
name();
name(22, 55);
对象的简写
const name = "悟空";
const num = 111;
const obj = {
name,
num,
};
console.log(obj);
console.log(obj.name);
console.log(obj.num);
const obj1 = {
say() {
console.log("say what?");
},
};
console.log(obj1);
console.log(obj1.say);
解构
- 数组解构
const arr = ["悟空", "八戒", "龙马", "三藏"];
const [a,b,c,d] = arr
console.log(a,b,c,d);
- 对象解构
const obj = {
name: '悟空',
age: 5000
}
const { name, age } = obj;
console.log(name, age);
- 解构+默认值
const arr1 = [1]
const [a,b] = arr1
console.log(a,b);
const arr2 = [1];
const [a, b = 100] = arr;
console.log(a, b);
const obj1 = {
username: "小明",
height: 500,
};
const { username, height = 200 } = obj;
console.log(username, height);
拓展/剩余/延展运算符
- 获取剩余数组
const [a,b,...c] = [1,2,3,4,5,6]
console.log(c);
const [a,b,...c] = [1,2]
console.log(c);
- 获取剩余对象
const {a,...c} = {a:1,b:2,c:3}
console.log(c);
const { a, b, ...c } = { a: 1, b: 2 };
console.log(c);
- 伪数组转真数组
const lis = document.querySelectorAll("li");
const newLis = [...lis];
- 获取剩下 用在函数的形参上
function total(...args) {
let sum = 0;
args.forEach((value) => (sum += value));
}
total(1, 2);
total(1, 2, 3);
total(1, 2, 3, 4);
数组去重
for循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no"
/>
<title>21-数组去重-for循环</title>
</head>
<body>
<input type="text" />
<ul></ul>
<script>
const input = document.querySelector("input");
const ul = document.querySelector("ul");
const arr = ["a", "b"];
render();
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
let isHas = false;
for (let index = 0; index < arr.length; index++) {
if (arr[index] === this.value) {
isHas = true;
break;
}
}
if (isHas) {
console.log("有重复");
} else {
arr.push(this.value);
render();
}
}
});
function render() {
const html = arr.map((value) => `<li>${value}</li>`).join("");
ul.innerHTML = html;
}
</script>
</body>
</html>
for+splice
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no"
/>
<title>21-数组去重-for循环+splice</title>
</head>
<body>
<input type="text" />
<ul></ul>
<script>
const input = document.querySelector("input");
const ul = document.querySelector("ul");
let arr = ["a", "b"];
render();
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
let i = -1;
for (let index = 0; index < arr.length; index++) {
if (arr[index] === this.value) {
i = index;
break;
}
}
if (i === -1) {
} else {
arr.splice(i, 1);
}
arr.push(this.value);
render();
}
});
function render() {
const html = arr.map((value) => `<li>${value}</li>`).join("");
ul.innerHTML = html;
}
</script>
</body>
</html>
some
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no"
/>
<title>21-数组去重-some</title>
</head>
<body>
<input type="text" />
<ul></ul>
<script>
const input = document.querySelector("input");
const ul = document.querySelector("ul");
const arr = ["a", "b"];
render();
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
const isHas = arr.some((value) => value === this.value);
if (isHas) {
console.log("有重复了 不要再添加");
} else {
arr.push(this.value);
render();
}
}
});
function render() {
const html = arr.map((value) => `<li>${value}</li>`).join("");
ul.innerHTML = html;
}
</script>
</body>
</html>
filter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no"
/>
<title>21-数组去重-filter</title>
</head>
<body>
<input type="text" />
<ul></ul>
<script>
const input = document.querySelector('input');
const ul = document.querySelector('ul');
let arr = ['a', 'b'];
render();
input.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
const newArr = arr.filter((value) => value !== this.value);
arr = newArr;
arr.push(this.value);
render();
}
});
function render() {
const html = arr.map((value) => `<li>${value}</li>`).join('');
ul.innerHTML = html;
}
</script>
</body>
</html>