web-interview icon indicating copy to clipboard operation
web-interview copied to clipboard

[js] 74.js 中 this 闭包 作用域

Open qiilee opened this issue 6 years ago • 1 comments

答案:

this:指向调用上下文

闭包:定义一个函数就开辟了一个局部作用域,整个 js 执行环境有一个全局作用域

作用域:一个函数可以访问其他函数中的变量(闭包是一个受保护的变量空间)

var f = (function fn() {
  var name = 1;
  return function () {
    name++;
    console.log(name)
  }
})()

==>undefined 有疑问

qiilee avatar Sep 30 '19 01:09 qiilee

var a = f() ==> 2

webzmj530 avatar May 19 '20 03:05 webzmj530