前端前端
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
  • parseInt
  • preventDefault报错
  • promise
  • requestIdleCallback
  • undefined
  • webworker
  • 代码块
  • 前后端自动刷新
  • 多个标签页通讯
  • 实现个简单爬虫
  • 执行上下文
  • 控制台
  • 查找文件中的图片并下载
  • 深度广度优先
  • 累加函数
  • 触摸事件和键盘事件

执行上下文

转

执行上下文 {
变量对象
作用域链 -> 函数的作用域在函数定义的时候就决定了
this
}

this

调用场景
var obj = {a: 1, b: function(){console.log(this);}}

  1. 作为对象调用时,指向该对象 obj.b(); // 指向obj
  2. 作为函数调用, var b = obj.b; b(); // 指向全局window
  3. 为构造函数调用 var b = new Fun(); // this指向当前实例对象
  4. 作为call与apply调用 obj.b.apply(object, []); // this指向当前的object

规范解读

var value = 1;

var foo = {
  value: 2,
  bar: function () {
    return this.value;
  }
}

//示例1
console.log(foo.bar()); // 2
//示例2
console.log((foo.bar)()); // 2
//示例3
console.log((foo.bar = foo.bar)()); // 1
//示例4
console.log((false || foo.bar)()); // 1
//示例5
console.log((foo.bar, foo.bar)()); // 1

var Reference = { base: foo, name: 'bar', strict: false };

  1. 计算 MemberExpression 计算结果 foo.bar(就是()前面的)

  2. foo 是 reference 类型 IsPropertyReference(ref) 是 true (base value 是个对象,IsPropertyReference true)

  3. this = GetBase(ref)

  4. 运算符导致 getValue(ref), 一旦getValue, ref就不是 reference, this undefined, 隐私转换全局对象

其他

this 实际上是在函数被调用时建立的一个绑定,它指向 什么 是完全由函数被调用的调用点来决定的。

转

Edit this page
最近更新:: 2021/8/12 16:22
Contributors: wuhui
Prev
实现个简单爬虫
Next
控制台