批改状态:合格
老师批语:
let d = 10;let fn = function(a, b) {// a, b, c 都是私有变量// d 就是自由变量let c = 5;return a + b + c + d;};console.log(fn(1, 2));//18
fn = function(a) {// a = 10;// 1. 父子函数, f: 子函数let f = function(b) {// b = 20;return a + b;};// 2. 返回一个子函数return f;};let f = fn(10);// fn()调用完成,但是内部的a被子函数引用了, 所以fn()创建的作用域不消失console.log(typeof f);//functionconsole.log(f(20));//30
fn = function(a, b, c) {return a + b + c;};console.log(fn(1, 2, 3)); //6console.log(fn(1, 2));fn = function(a, b) {return function(c) {return a + b + c;};};// 使用闭包, 可以将三个参数分2次传入f = fn(1, 2);console.log(f(3)); //6
// 外部变量let discound = 0.8;function getPrice(price, discound = 1) {// 纯函数中禁用有自由变量return price * discound;}console.log(getPrice(12000, discound));//9600);
// 访问器属性let address = {data: { province: "江苏", city: "南京" },get province() {return this.data.province;},set province(province) {this.data.province = province;},};//读console.log(address.province); //江苏//写address.province = "江苏省";console.log(address.province); //江苏省
三.类与对象的创建与成员引用
let User = function(name, email) {// 1. 创建一个新对象// let this = new User;// 2. 给新对象添加自定义的属性this.name = name;this.email = email;// 3. 返回 这个新对象// return this;// 以上, 1, 3 都是new的时候,自动执行, 不需要用户写};const user1 = new User("admin", "admin@php.cn");console.log(user1);
let User = function(name, email) {this.name = name;this.email = email;};const user1 = new User("admin", "admin@php.cn");User.prototype.getInfo = function() {return `name = ${this.name}, email = ${this.email}`;};console.log(user1.getInfo()); //name = admin, email = admin@php.cn
四. 数组与对象的解构过程与经典案例
//模板 = 数组let [name, email] = ["朱老师", "498668472@qq.com"];console.log(name, email);
//对象模板 = 对象字面量let { id, lesson, score } = { id: 1, lesson: "js", score: 80 };console.log(id, lesson, score);let {...obj } = { id: 1, lesson: "js", score: 80 };console.log(obj);
function getUser({id,name,email}) {console.log(id,name,email);}getUser({id:123, name:'张三',email:'zs@php.cn'})
五. JS引入到浏览器中的的方法
<!DOCTYPE html><html lang="zh-CN"><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>浏览器中的js</title></head><body><!-- 1. 事件属性, 写到元素的事件属性 --><button onclick="console.log('hello world');">按钮1</button><!-- 2. 事件属性, 写到元素的事件属性 --><button onclick="setBg(this)">按钮2</button><script>function setBg(ele) {document.body.style.backgroundColor = "wheat";ele.style.backgroundColor = "yellow";ele.textContent = "保存成功";}</script></body></html>
六. 获取DOM元素的API
<!DOCTYPE html><html lang="zh-CN"><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>获取dom元素</title></head><body><ul class="list"><li class="item">item1</li><li class="item">item2</li><li class="item">item3</li><li class="item">item4</li><li class="item">item5</li></ul><script>//将所有的item变成红色console.log(document);const items = document.querySelectorAll(".list > .item");console.log(items);for (let i = 0, length = items.length; i < length; i++) {items[i].style.color = "red";}//将第一个改为黄色背景const first = document.querySelector(".list > .item");console.log(first === items[0]);first.style.backgroundColor = "yellow";//将第三个改为绿色背景const three = document.querySelector(".list > .item:nth-of-type(3)");three.style.backgroundColor = "green";// 快捷方式//bodyconsole.log(document.querySelector("body"));console.log(document.body);//headconsole.log(document.head);// titleconsole.log(document.title);// htmlconsole.log(document.documentElement);</script></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号