Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
1.父子函数
2.子函数调用了父函数中的变量
fn = function (a) {
let f = function (b) {
return a + b;
};
return f;
};
let f = fn(10);
console.log(typeof f);
console.log(f(20));
经典应用
闭包:偏函数(参数分批传入,柯里化函数)
fn = function (a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
};
console.log(fn(1)(2)(3));
反闭包:纯函数
纯函数:函数中用到的变量全是自己的,没有自由变量
如果用到外部变量,传入参数
let discound = 0.8;
function getPrice(price, discound = 1) {
return price * discound;
}
console.log(getPrice(1200, discound));
访问器属性:进行属性伪装,将一个方法伪装成属性进行访问
user = {
data: { name: "李同学", height: 160 },
get height() {
return this.data.height;
},
set height(height) {
if (height >= 150 && height <= 200) {
this.data.height = height;
} else {
console.log("不正常");
}
},
};
console.log(user.height);
user.height = 165;
console.log(user.height);
1.数组解构 模板=数组
let [name, email] = ["朱老师", "364967906@qq.com"];
console.log(name,email);
2.对象解构 对象模板=对象字面量
let{id,lesson,score}={id:1,lesson:'js',score:80}
console.log(id,lesson,score);
3.应用场景
function getUser({id,name,email}){
console.log(id,name,email);
}
getUser({id:123,name:'li',email:'123@php.cn'})
1.事件属性,写到元素的事件属性<button onclick="console.log('hello world');">按钮</button>
2.使用script标签引入js脚本,写到标签中,仅于当前的文档
<button onclick="setBg(this)">按钮2</button>
<script>
function setBg(ele) {
document.body.style.backgroundColor = "wheat";
ele.style.backgroundColor = "yellow";
ele.textContent = "保存成功";
}
</script>
3.如果这个按钮功能,需要多个页面使用,可以将js脚本保存为外部脚本,然后再引入HTML中<script src="out.js"></script>
1.一组:querySelectorAll(css选择器)
console.log(document);
const items = document.querySelectorAll(".list>.itme");
console.log(items);
for (let i = 0; i < items.length; i++) {
items[i].style.color = "red";
}
2.一个:querySelector(css选择器)
const first = document.querySelector(".list>.itme");
console.log(first);
first.style.backgroundColor = "yellow";