批改状态:合格
老师批语:
// 有名字的函数// 命名: 动 + 名// 声明function getName(username) {let a = "猪";return "Hello " + a + username;}// 调用console.log(getName("老师")); // Hello 猪老师
//执行方式1: 立即执行//IIFE(function(username) {console.log("Hello " + username);})("灭绝老师");console.log((function(username) {return "Hello " + username;})("灭绝老师"));
// 执行方式2: 保存到一个变量中// 函数表达式const getUserName = function(username) {return "Hello " + username;};console.log(getUserName("马老师")); //Hello 马老师
使用 箭头函数 来简化匿名函数的声明
let f1 = function(a, b) {return a + b;};// 1. 去掉 function// 2. 在参数括号(...) 与 大括号{...} 之间使用 胖箭头 => 连接f1 = (a, b) => {return a + b;};// 如果只有一条语句,return ,可以不写大括号f1 = x => x * 2;console.log(f1(40));
| 值 | 类型 | 举例 |
|---|---|---|
| 数值 | number | 123 |
| 字符串值 | string | php |
| 布尔值 | boolean | true |
| undefined值 | undefined | undefined |
| object值 | null | null |
//数值numberconsole.log(123, typeof 123);//字符串值stringconsole.log("php", typeof "php");//布尔值booleanconsole.log(true, typeof true);//undefined值console.log(undefined, typeof undefined);//object值console.log(null, typeof null);
| 值 | 类型 | 举例 |
|---|---|---|
| 数组 | array | [“phone”, “computer”, “car”] |
| 对象 | object | {phone:”huawei”, compute:”联想”, car:”小鹏”} |
| 函数 | function | total:function(){} |
//数组const arr = ["手机", 2, 5000];// 数组的索引是从0开始递增的正整数, 0, 1, 2console.log(arr[0]);//手机console.log(arr[1]);//2console.log(arr[2]);//5000//对象let obj = { name: "手机", num: 2, price: 5000 };// 对象 更像一个语义化的 数组console.log(obj["name"]);// 手机console.log(obj["num"]);//2console.log(obj["price"]);//5000//函数// 对象最吸引人的,不是数组的语义化封装, 而是对数据操作的封装, 方法(语法与函数是一样的)// 本质上来说, 对象, 就是变量与函数的封装, 内部, 变量->属性, 函数 -> 方法obj = {name: "手机",num: 3,price: 7000,// total: 方法,实际上还是一个属性,只不过它的值是一个函数total: function () {let str = `${this.name} 总计 ${this.num * this.price} 元`;return str;},};console.log(obj.total());//手机 总结 21000 元
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号