批改状态:合格
老师批语:
<button onclick="show(this)">Click me</button>
<script>const btn = document.querySelector('button');btn.addEventListener('click', show);function show(ev) {console.log(ev.target.innerHTML);ev.target.style.background = 'lightskyblue';}</script>
<script src="js.js"></script>
<script>//一行声明并初始化赋值let name = 'jack';//声明未初始化赋值,默认值underfindlet name;</script>
<script>const APP = '系统';</script>
//1.蛇形,下划线let user_name;//小驼峰,从第二个单词开始首字母大写let userName;//大驼峰,所有单词的首字母大写let UserName;
let age = 20;
let name = 'jack';
let request = true;
let name;
let request = null;
let man = {//属性,相当于变量name = 'jack',age = 20,gander = 'man'//方法,相当于函数getName: function () {// this表示当前的上下文,当前对象return '我的名字:' + this.name;}}
let arr = [1,2,3,4];//获取第三个元素console.log(arr[2]);//添加元素console.log((arr[4] = 99));//修改元素console.log((arr[0] = 100));
function hello(a, b, c);console.log(arguments);//添加元素hello.d = 'd';
//字符串'100'在运算的时候会转换成数值类型console.log(100 + '100');//输出100100
类型匹配
console.log(100 == '100');//输出 true
console.log(100 === '100');//输出 false
//调用可以写在函数声明前name();function name() {console.log("我叫:jack");}
//将函数赋值给一个常量const sum = function (a, b) {return a + b;};
function demo2(...arr) {console.log(arr);}demo2(1, 2, 3, 4, 5);
function add1(...arr) {// console.log(arr);let res = 0;for (let num of arr) {res += num;}console.log('计算结果:',res);}add1(1, 2, 3, 4, 5, 6, 7);//输出计算结果:28
let arr = [1,2,3,4,5,6];console.log(...arr);
function demo5() {return {status: 1,message: "成功",};}let res = demo5();console.log(res.status, res.message);
let name = 'jack';let age = 18;let gender = 'man';let num;//完全写法function demo6() {return [name, age, gender];}res = demo6();console.log(res);//用箭头函数简写,可以省略function 和return及{}let demo6 = () => [name, age, gender];res = demo6();console.log(res);//如果只有一个参数可以省略()let demo6 = num => [name, age, gender];res = demo6(num);console.log(res);
document.addEventListener("click", function () {alert("Hello World~~");});
let sum = function (a, b) {return function (c, d) {return a + b + c + d;};};let f1 = sum(1, 2);console.log(f1(3, 4));
sum = function (a) {return function (b) {return function (c) {return function (d) {return a + b + c + d;};};};};// 简化了调用参数let res = sum(10)(20)(30)(40);console.log(res);
//纯函数function add(a, b) {console.log(a + b);}add(1, 2);//不是纯函数function getDate() {return Date.now();}console.log(getDate());
let name = "jack";
function name() {//函数私有变量let age = 18;console.log("我叫:jack");}
{let name = 'Tom';}
能够访问自由变量的函数,所以理论上讲,任何函数都是闭包
自由变量: 即不是函数参数变量也不是私有变量,存在于函数调用上下文中
let num = 100;function add(a, b) {// a,b: 参数变量// t: 私有变量let t = 0;// num: 自由变量return t + a + b + num;}
function f1() {let a = 1;// a 相对于f1是私有变量,但是相对于返回的匿名函数就是一个自由变量return function () {return a++;};}//把函数赋给一个变量let f2 = f1();//输出f1的匿名函数console.log(f2());
//函数用()包含起来,并把调用直接跟随在后边(function sum(a, b) {console.log(a + b);})(1, 2);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号