1.常用函数类型
1.1命名函数
1.2匿名函数
1.3箭头函数:箭头函数是用来简化匿名函数的。
<!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>函数</title>
</head>
<body>
<script>
// 函数:一般当成数据类型使用
// 1.命名函数
function gerName(username) {
return "你好," + username;
}
console.log(gerName("朱老师"));
// 2.匿名函数
// 第一种使用方式:赋给一个变量
let userName = function (username) {
return "你好," + username;
};
console.log(userName("李老师"));
// 第二种使用方式:立即调用函数(IIFE),将声明与调用合并。
// 表达式:用一对括号括起来
console.log(
(function (username) {
return "你好," + username;
})("杨老师")
);
// 3.箭头函数:用来简化匿名函数
function sum(a, b) {
console.log(a + b);
}
sum(2, 3);
// 改造成箭头函数
// 第一步:改造成匿名函数
// let add = function (a, b) {
// console.log(a + b);
// };
// 第二步:使用箭头函数来简化
// 简化方法:删除function,在参数列表与大括号之间加入胖箭头:=>
add = (a, b) => {
console.log(a + b);
};
add(3, 10);
// 如果只有一个参数,参数括号可以不写
add = (a) => {
console.log(a + 15);
};
add(3);
// 如果没有参数,参数括号必须填写
add = () => {
console.log(2 + 2);
};
add();
// 如果函数体只有一条语句,大括号可以删除
add = () => console.log(2 + 8);
add();
// 示例
// let userName = function (username) {
// return "你好," + username;
// };
// 改造后:
let f1 = (username) => "你好," + username;
console.log(f1("张老师"));
</script>
</body>
</html>
2.常用数据类型
2.1 原始类型:number,string,boolean,undefined,null
2.2 引用类型:array,object,function
<!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>数据类型</title>
</head>
<body>
<script>
// 1.原始类型:number,string,boolean,undefined,null
// 一个变量一个值,标量
// 不同类型的数据,先转换后运算
console.log("hell " + 100);
console.log(typeof ("hell" + 100));
// 隐式转换:true => 1
console.log(true + 1);
console.log(typeof (true + 1));
// undefined
console.log(typeof undefined);
console.log(null);
console.log(typeof null);
// 2.引用类型:array,object,function
// 一个变量保存的是一个集合,不是单值,通过变量引用访问
// 引用类型判断不能用typeof来判断,用Array.isArray()
// 数组
const arr = [1, [1, 5, 6], "admin", true];
console.log(arr);
// 访问数组成员中的元素,必须引用数组来访问
console.log(arr[2]);
console.log(arr[1][2]);
// 判断是否是数组类型
console.log(Array.isArray(arr));
console.log(Array.isArray(arr) ? "数组" : "不是数组");
// 对象
let obj = {
id: 1,
jurisdiction: [1, 5, 6],
username: "朱老师",
isok: true,
};
console.log(obj["username"]);
console.log(obj["jurisdiction"][2]);
// 简化访问方式
console.log(obj.username);
console.log(obj.jurisdiction[2]);
// 示例
function getUser(obj) {
return "id = " + obj.id + ",username = " + obj.username;
}
console.log(getUser(obj));
// 对象是可以将数据和函数封装在一起,作为一个独立的编程单元
// 对象字面量
obj = {
id: 1,
jurisdiction: [1, 5, 6],
username: "朱老师",
isok: true,
// 将一个函数转为对象的方法,封装到对象中
getUser: function (obj) {
return "id = " + obj.id + ",username = " + obj.username;
},
};
console.log(obj.getUser(obj));
console.log("-------------");
// 在对象中,使用变量this来引用对象自身
obj2 = {
id: 1,
jurisdiction: [1, 5, 6],
username: "朱老师",
isok: true,
getUser: function () {
return "id = " + this.id + ",username = " + this.username;
},
};
console.log(obj2.getUser());
console.log("--------------");
// 函数:函数是对象,也是一个值,可以当作参数传递,也可以当成返回值
// 示例
console.log(typeof function () {});
function f2(callback) {
console.log(typeof callback);
console.log(callback());
}
// 传入一个函数作为参数
f2(function () {
return "Hell, How are you?";
});
// 函数当返回值:调用不属于自己的参数,形成闭包,闭包不销毁父函数,占用内存较大
function f3() {
let a = 1;
return function () {
return (a = a + 1);
// return (a += 1);
// return (a += 3);
// return a++;
};
}
// 调用后作用域不被销毁
console.log(f3());
const f = f3();
console.log("-----");
console.log(f());
// 父函数作用域不注销,每次递增
console.log(f());
// 函数就是对象,对象可以添加属性和方法
// f4是函数
let f4 = function () {};
// 添加属性
f4.myEmail = "652@php.cn";
console.log(f4.myEmail);
// 添加方法测试
f4.getEmail = function () {
console.log(this.myEmail);
};
f4.getEmail();
</script>
</body>
</html>