实例演示四种常用的函数类型
<script>
// 命名函数
let r = 8;
const PI = 3.14;
console.log(square(r));
function square(radio) {
return (
`命名函数计算半径为${radio}cm 的圆形面积为 ` +
PI * radio * radio +
` cm^2`
);
}
// 匿名函数
let square1 = function (r) {
return `匿名函数计算半径为${r}cm 的圆形面积为 ` + PI * r * r + ` cm^2`;
};
console.log(square1(5));
// 箭头函数
let square2 = (r) => {
return `箭头函数计算半径为${r}cm 的圆形面积为 ` + PI * r * r + ` cm^2`;
};
console.log(square2(9));
// 立即执行函数
let square3 = (function () {
return (
`立即执行函数计算半径为${r}cm 的圆形面积为 ` + PI * r * r + ` cm^2`
);
})(12);
console.log(square3);
</script>
实际演示原始数据类型与引用类型
let uname = "mike";
let age = 28;
let gender = null;
let mail;
let student = [uname, age, gender, mail];
console.table(student);
let workmate = { name: uname, age: age, gender: gender, mail: mail };
console.dir(workmate);
let ismale = workmate.gender == "male";
console.log(
workmate.name +
"'s age is " +
workmate["age"] +
". gender is male? " +
ismale
);