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/12-this-keyword/answer.js
Normal file
26
03-javascript-core/12-this-keyword/answer.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const student = {
|
||||
name: "林晨",
|
||||
stage: "JavaScript 核心",
|
||||
sayHello() {
|
||||
const currentName = this && this.name ? this.name : "未知调用者";
|
||||
const currentStage = this && this.stage ? this.stage : "未知阶段";
|
||||
console.log(`你好,我是 ${currentName},正在学习 ${currentStage}`);
|
||||
},
|
||||
createArrowReporter() {
|
||||
return () => {
|
||||
console.log(`箭头函数继承到的 this.name:${this.name}`);
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
student.sayHello();
|
||||
|
||||
const detachedHello = student.sayHello;
|
||||
detachedHello();
|
||||
|
||||
const arrowReporter = student.createArrowReporter();
|
||||
arrowReporter();
|
||||
|
||||
console.log("对象方法里的 this 通常指向调用它的对象。");
|
||||
console.log("脱离对象单独调用后,普通函数里的 this 会跟着调用方式变化。");
|
||||
console.log("箭头函数没有自己的 this,它会继承创建它时外层的 this。");
|
||||
Reference in New Issue
Block a user