Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<!DOCTYPE html>
<html lang="en">
<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>javascript数据类型和分支循环</title>
</head>
<body>
<script>
// javascript数据类型分为
// 1、原始类型: Number String Boolean undefined null
// 2、引用类型:array object function
// number
console.log("这是number:" + 123);
//string
console.log("这是string:" + "123");
//boolean
console.log("这是boolean:" + false);
//undefined
let a;
console.log("这是undefined:" + a);
//null
let b = null;
console.log("这是null:" + b);
//array
let arr = ["小明", "男", "16", "16cm"];
console.log("这是array:" + arr);
let obj1 = [
{ name: "手机", num: 2, price: 5000 },
{ name: "电脑", num: 5, price: 6000 },
{ name: "相机", num: 4, price: 2000 },
];
console.log("这是array:" + obj1[0].name);
//object
let obj = {
name: "Tom",
age: 18,
height: "180cm",
fun: function () {
console.log("这是一个function.");
},
};
console.log("这是一个object:" + obj["age"]);
console.log("这是一个object:" + obj.age);
obj.fun();
//查看数据类型用typeof
console.log(typeof function () {});
console.log(arr instanceof Object);
//回调函数
function f1() {}
function f2(f1) {
console.log("这是回调函数" + typeof f1);
}
f2(f1);
//这是闭包函数
function f3() {
return function () {
return "hello world";
};
}
console.log("这是闭包函数" + f3()());
</script>
<script>
//分支和循环
// 1、单分支
if (true) {
console.log("这是一个单分支");
}
//2、双分支
if (false) {
console.log("这个是双分支,真的");
} else {
console.log("这个是双分支,假的");
}
//3、三元运算
let res;
isNaN(123) ? (res = "是") : (res = "不是");
console.log("这" + res + "三元运算");
//多分支
let age = 20;
if (age < 18) {
console.log("年龄小于18岁");
} else if (age >= 18 && age < 25) {
console.log("正是青春年华");
} else if (age >= 25) {
console.log("差不多了");
}
//switch判断分支
//必须要用true做为switch入口判断条件
switch (true) {
case age < 18:
console.log("年龄小于18岁");
breack;
case age >= 18 && age < 25: {
console.log("正是青春年华");
break;
}
default: {
console.log("差不多了");
}
}
//for循环,for of 循环 ,for in 循环
arr = [9, 4, 2, 7, 4];
obj = {
product: "电视",
num: 3,
price: 300,
fun: function () {},
};
//for循环
for (let i = 0; i < arr.length; i++) {
console.log("for循环:" + arr[i]);
}
//for of 循环
for (let item of arr) {
console.log("for of循环:" + item);
}
for (let item of arr.values()) {
console.log("for of循环值:" + item);
}
for (let item of arr.keys()) {
console.log("for of循环键:" + item);
}
//for in 循环
for (let key in obj) {
console.log("for in循环键:" + key);
}
//这里只能发obj[key]访问,不能用obj.key .obj[key],key不能有引号
for (let key in obj) {
console.log("for in循环值:" + obj[key]);
}
</script>
</body>
</html>