批改状态:合格
老师批语:
1.命名函数
//命名:动词+名词function getName(username) {return "Hello" + username;}console.log(getName("猪老师"));
2.匿名函数
没有名字的函数
执行方式:2.1.立即执行:IIFE
(function (username) {console.log("Hello" + username);})("灭绝老师");console.log((function (username) {return "Hello" + username;})("灭绝师妹"));
IIFE:阅后即焚,不会给全局环境带来任何污染,用来创建临时作用域
node 模块,就是用IIFE来写
执行方式2.保存到变量中
2.2.函数表达式
const getUserName = function (username) {return "Hello" + username;};console.log(getUserName("马老师"));
2.3.箭头函数
使用箭头函数简化匿名函数的声明
1.去掉function
2.在参数括号与大括号之间加胖箭头
let f1 = function sum(a, b) {return a + b;};console.log(f1(10, 20));f1 = (a, b) => {return a + b;};console.log(f1(8, 9));//只有一个参数时()可以不写f1 = username => {return "hello" + username;};//只有一条语句,return是默认的,return可以不写f1 = x => x * 2;console.log(f1(4));//没有参数()不能省f1 = () => "jintiantiaoqi";console.log(f1());
1.原始类型
number string boolean underfined null
console.log(123, typeof 123);console.log("php", typeof "php");console.log(true, typeof true);console.log(undefined, typeof undefined);console.log(null, typeof null);
2.引用类型
array object function
2.1 array
const arr = ["手机", 2, 5000];console.log(arr);console.log(arr[0]);console.log(arr[1]);console.log(arr[2]);console.log(typeof arr);console.log(Array.isArray(arr));
2.2 object
console.log(123, typeof 123);console.log("php", typeof "php");console.log(true, typeof true);console.log(undefined, typeof undefined);console.log(null, typeof null);
2.3 function
2.3.1 value
function f2() {let a = 1;return function () {return a++;};}console.log(f2());const f = f2();console.log(f());console.log(f());console.log(f());console.log(f());console.log(f());console.log(f());
2.3.2 object
function f1(callback) {console.log(callback());}f1(function () {return "hell 朱老师";});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号