字节跳动二面有关于变量提升和this指向的问题
发布日期:2021-05-12 21:18:25 浏览次数:14 分类:精选文章

本文共 1729 字,大约阅读时间需要 5 分钟。

������������������������������������������this������������

������������������

��� JavaScript ������������������������������������������������������������������������������

// ������������
console.log(a); // fun a//2
var a = 1;
function a() {
// 1
}
a = 2;
function a() {
// 2
}
console.log(a); // 2

���������������������

  • ������������������ a ������������������������
  • ������ a ��������������������� 2

������������������������������

function a() {
// 1
}
function a() {
// 2
}
var a;
console.log(a); // function a { //2 }
a = 1;
a = 2;
console.log(a); // 2

������this������������������������

��������������� this ��������������������������� this������������������������������������

// this
var name = "window";
var bar = {
name: "bar"
};
var foo = {
name: "foo",
say: () => {
console.log(this.name)
},
say2: function() {
return () => {
console.log(this.name)
}
}
};
  • foo.say()���������������������������this ��������� window���
  • foo.say.call(bar)��������������� call ���������this ��������� bar���
  • foo.say2()������������������������������������������������this ��������� foo���
  • foo.say2.call(bar)��������������� call ���������this ��������� bar��������������������������������������������������� this������

������������������

  • ��������������������������� call���apply ��� bind ������������������������������������������������������ this ������������������������
  • ��������������������������������������������� foo.say2()��������������� this ������������������������������ this ������������������

������������

  • foo.say()���window
  • foo.say2()���foo
  • foo.say2.call(bar)���bar

��������������������������������������������������������������������� this ���������������

上一篇:React学习笔记——类中的方法内部的this指向
下一篇:字节跳动二面题目总结

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月22日 18时17分03秒