feat: Add JavaScript core exercises and solutions
- Implemented exercises for array high-order methods, memory and execution, switch statements, and final review. - Added starter and answer files for each exercise to facilitate learning. - Created a runner HTML file to execute JavaScript code and display console outputs. - Updated README files to include exercise objectives, tasks, and usage instructions.
This commit is contained in:
26
03-javascript-core/14-memory-and-execution/README.md
Normal file
26
03-javascript-core/14-memory-and-execution/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# 练习 14:内存和执行
|
||||
|
||||
## 目标
|
||||
|
||||
理解“值”和“引用”的区别,以及函数执行时数据为什么会变化。
|
||||
|
||||
## 你要练什么
|
||||
|
||||
- 基本类型复制
|
||||
- 引用类型共享
|
||||
- 函数执行顺序
|
||||
- 参数传递
|
||||
|
||||
## 任务
|
||||
|
||||
请完成一个“数据变化观察”脚本,要求:
|
||||
|
||||
- 比较基本类型复制后是否互相影响
|
||||
- 比较对象复制后是否互相影响
|
||||
- 写两个函数观察执行顺序
|
||||
- 写一个函数修改传入对象,观察外部对象为什么会变化
|
||||
|
||||
## 文件
|
||||
|
||||
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/14-memory-and-execution/starter.js)
|
||||
- [answer.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/14-memory-and-execution/answer.js)
|
||||
39
03-javascript-core/14-memory-and-execution/answer.js
Normal file
39
03-javascript-core/14-memory-and-execution/answer.js
Normal file
@@ -0,0 +1,39 @@
|
||||
let scoreA = 80;
|
||||
let scoreB = scoreA;
|
||||
|
||||
scoreB = 95;
|
||||
|
||||
console.log("基本类型 scoreA:", scoreA);
|
||||
console.log("基本类型 scoreB:", scoreB);
|
||||
console.log("基本类型复制后,两个变量互不影响。");
|
||||
|
||||
const userA = {
|
||||
name: "小周",
|
||||
city: "上海",
|
||||
};
|
||||
|
||||
const userB = userA;
|
||||
userB.city = "深圳";
|
||||
|
||||
console.log("对象 userA:", userA);
|
||||
console.log("对象 userB:", userB);
|
||||
console.log("对象变量保存的是引用,所以改 userB 会影响 userA。");
|
||||
|
||||
function printStepOne() {
|
||||
console.log("步骤一:先进入第一个函数");
|
||||
}
|
||||
|
||||
function printStepTwo() {
|
||||
console.log("步骤二:再执行第二个函数");
|
||||
}
|
||||
|
||||
function updateUser(user) {
|
||||
user.city = "杭州";
|
||||
console.log("函数内部修改后的 user:", user);
|
||||
}
|
||||
|
||||
printStepOne();
|
||||
printStepTwo();
|
||||
updateUser(userA);
|
||||
|
||||
console.log("函数执行结束后的 userA:", userA);
|
||||
28
03-javascript-core/14-memory-and-execution/starter.js
Normal file
28
03-javascript-core/14-memory-and-execution/starter.js
Normal file
@@ -0,0 +1,28 @@
|
||||
let scoreA = 80;
|
||||
let scoreB = scoreA;
|
||||
|
||||
const userA = {
|
||||
name: "小周",
|
||||
city: "上海",
|
||||
};
|
||||
|
||||
const userB = userA;
|
||||
|
||||
function printStepOne() {
|
||||
console.log("步骤一");
|
||||
}
|
||||
|
||||
function printStepTwo() {
|
||||
console.log("步骤二");
|
||||
}
|
||||
|
||||
function updateUser(user) {
|
||||
// 任务:
|
||||
// 1. 修改 user.city
|
||||
}
|
||||
|
||||
// 任务:
|
||||
// 2. 修改 scoreB,观察 scoreA 是否变化
|
||||
// 3. 修改 userB.city,观察 userA.city 是否变化
|
||||
// 4. 按顺序调用 printStepOne 和 printStepTwo
|
||||
// 5. 调用 updateUser(userA)
|
||||
Reference in New Issue
Block a user