批改状态:合格
老师批语:
<script>// 对象字面量简化let grade1 = {// 属性code1: '48H',// 方法greet1: function(){return `${this.code1}, 开发成功`},}console.log(grade1.greet1())// 简化code2 = '52H'let grade2 = {// 属性简化code2,// 方法简化 去掉:和functiongreet1(){return `${this.code2}, 开发成功`},// 箭头函数,去掉function,只有一行return可以省略return和{}greet2:()=>`${grade2.code2}, 牛!`,}console.log(grade2.greet1())console.log(grade2.greet2())// 多分支// let sex = 'male'let sexif(sex=='male'){console.log('帅哥,里面请!')}else if(sex == 'female'){console.log('美女,您好!')}else{console.log('客人,请进!')}// 二分支语法糖const isVip = trueconsole.log(isVip ? '欢迎观看':'对不起,请开通Vip')// 多分支语法糖let date = 6switch(date){case 7: console.log('今天星期天,睡个懒觉!')break;case 6: console.log('忙了一周该休息了!')breakdefault: console.log('赶紧工作!')}// for循环let sum = 0arr = [1,2,3,4,5,6,7,8,9]for(let i = 0; i < arr.length; i++){for(let j = 0; j <= i; j++){sum += arr[i] * arr[j]}}console.log(sum)// for-of循环遍历数组valuelet N52H = ['nd', 'fe',' b']for(let value of N52H){console.log(value)}// 数组不能用遍历key// for(let key of N52H){// console.log(value)// }// for-in遍历对象key及valuelet N48H = {nd: 31, fe:68, b:1}for(let key in N48H){console.log(key)}for(let key in N48H){console.log(N48H[key]) // 不能用.语法}for(let value in N52H){console.log(value)}// forEach遍历数组N52H.forEach(item =>console.log(item))// map遍历数组,有返回值,而不是执行N54H = [4,5,6]let res = N54H.map(item =>item * 2)console.log(res)grade = ['N48H', 'N50H', 'N52H']arr = grade.map(item=>`<li><a href="">${item}</a></li>`)res = arr.join('')res = `<ul>`+res+`</ul>`console.log(res)// DOM渲染document.body.insertAdjacentHTML('afterbegin',res)// 参数不足是给默认值sum = (a, b=0)=>`${ a + b}`sum = (...arr) =>arrconsole.log(sum(1,2,3,4)) //不丢参数sum = (a, b,...arr) =>console.log(a,b,arr) //剩余参数压缩在arrsum = (a, b,...arr) =>console.log(a,b,...arr) //参数展开sum(9,8,7,6,5)// 求和sum = (...arr)=>{let acc = 0for(i=0;i<arr.length;i++){acc+= arr[i]}return acc}console.log(sum(9,8,7,6,5))// 返回值为对象是需要加(),不然{}容易混淆// 数组解构let [name, salary] = ['Tom', 5000]console.log(name, salary)//更新;[name, salary] = ['Tom', 6000]console.log(name, salary);[name, salary, ...arr] = ['Tom', 6000, 20, 8000]console.log(name, salary, ...arr)// 对象解构let {uname:姓名, salary1} = {uname:'Tom', salary1:9000}//变量名必须与key一致console.log(姓名, salary1)// 应用场景 克隆let N35EH = {code:'52p', dy:2}let{...obj}=N35EH //变量obj就是N35EH的克隆// 解构传参let show = function(N35EH){return `${N35EH.code}:${N35EH.dy}%`}console.log(show({code:'52p', dy:3}))// 简化的对象解构传参show = function({code, dy}){return `${code}:${dy}%`}console.log(show({code:'52p', dy:4}))// 箭头简化show = ({code, dy})=> `${code}:${dy}%`console.log(show({code:'52p', dy:5}))</script>
323.html:20 48H, 开发成功0323.html:34 52H, 开发成功0323.html:35 52H, 牛!0323.html:45 客人,请进!0323.html:49 欢迎观看0323.html:55 忙了一周该休息了!0323.html:68 11550323.html:73 nd0323.html:73 fe0323.html:73 b0323.html:83 nd0323.html:83 fe0323.html:83 b0323.html:86 310323.html:86 680323.html:86 10323.html:89 00323.html:89 10323.html:89 20323.html:93 nd0323.html:93 fe0323.html:93 b0323.html:98 (3) [8, 10, 12]0323.html:104 <ul><li><a href="">N48H</a></li><li><a href="">N50H</a></li><li><a href="">N52H</a></li></ul>0323.html:111 (4) [1, 2, 3, 4]0323.html:113 9 8 7 6 50323.html:124 350323.html:129 Tom 50000323.html:132 Tom 60000323.html:134 Tom 6000 20 80000323.html:138 Tom 90000323.html:148 52p:3%0323.html:153 52p:4%0323.html:156 52p:5%
箭头函数挺简洁;for-of不能获取key,而for-in即可以获取key也可以获取value,不确定这个理解对不对。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号