Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this指向/变量声明提升/作用域链问题 #2

Open
Liqiuyue9597 opened this issue Jul 30, 2020 · 1 comment
Open

this指向/变量声明提升/作用域链问题 #2

Liqiuyue9597 opened this issue Jul 30, 2020 · 1 comment

Comments

@Liqiuyue9597
Copy link
Owner

Liqiuyue9597 commented Jul 30, 2020

function a(){
    this.b = 3
}
var c = new a()
a.prototype.b = 7
var b = 9
a()

//说出输出什么
console.log(b) 
console.log(c.b) 
var a=33;
function test(){
  console.log(a)
  var a='eee'
}
test() //输出什么
window.name = 'bytedance'
function A() {
this.name = '123'
}
A.prototype.getA = function () {
console.log(this)
return this.name + 1
}
let a = new A()
let funcA = a.getA
//输出什么
funcA() 
a.getA() 
@Liqiuyue9597 Liqiuyue9597 changed the title this指向/作用域/原型链问题 this指向/变量声明提升/作用域链问题 Aug 12, 2020
@Liqiuyue9597
Copy link
Owner Author

这道题考察的是变量提升中,函数是一等公民

var temp = 1;
function test() {
  temp = 10;
  return;
  function temp() {}
}
test();
console.log(temp);

temp的输出结果是1。
分析:因为在test函数里,function temp() {}首先被函数声明提升,此时temp = function (){} (类似于temp变量在函数内部被声明了),然后再执行语句 temp=10,相当于此时temp=10修改的是test函数作用域里的temp,没有修改外面的temp。所以在全局变量作用域里输出时输出的是全部变量temp,此时为1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant