如何在JavaScript中的箭头函数内访问'this'关键字?
箭头功能中的“this”关键字
JavaScript的'this'关键字引用了它所属的对象。在箭头函数中,“this”属于全局对象。在一个简单的函数中,“this”关键字可能会导致未定义,但在箭头函数中可能会导致一个精确值。
示例
<html>
<body>
<script>
function Student(fname, grade) {
this.fname = fname;
this.grade = grade;
this.details = function() {
return () => {
document.write(`Hi, I'm ${this.fname} from ${this.grade} grade`);
};
}
}
let info = new Student('picaso', 'seventh');
let printInfo = info.details(); printInfo();
</script>
</body>
</html>输出结果
Hi, I'm picaso from seventh grade
热门推荐
10 古风祝福语唯美简短亲情
11 祝福语女友文案简短霸气
12 六一加端午祝福语简短
13 工厂祝福语简短10字
14 新婚祝福语简短十六字
15 离别祝福语简短送同事
16 送给情侣贺卡祝福语简短
17 表达家人的祝福语简短
18 生日祝福语简短而有深意