如何在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 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短