-
Notifications
You must be signed in to change notification settings - Fork 456
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
下面这段代码的输出是什么? #24
Comments
**说明:**一道经典的面试题,仅是为了帮助大家回顾一下知识点,加深理解,真实工作中,是不可能这样写代码的,否则,肯定会被打死的。 1.首先预编译阶段,变量声明与函数声明提升至其对应作用域的最顶端。 因此上面的代码编译后如下(函数声明的优先级先于变量声明): function Foo() {
getName = function() {console.log(1)};
return this;
}
var getName;
function getName() {console.log(5)};
Foo.getName = function() {console.log(2)};
Foo.prototype.getName = function() {console.log(3)};
getName = function() {console.log(4)}; 2. 3. 4. 如果是严格模式,this 指向 undefined,此处会抛出错误。 如果是node环境中,this 指向 global,node的全局变量并不挂在global上,因为global.getName对应的是undefined,不是一个function,会抛出错误。 5. 6. 7. 8. 因此最终结果如下: Foo.getName(); //2
getName();//4
Foo().getName();//1
getName();//1
new Foo.getName();//2
new Foo().getName();//3
new new Foo().getName();//3 |
小姐姐对知识点的理解真透彻 很强~ |
小姐姐厉害,看了很多你的文章,很有帮助 |
The text was updated successfully, but these errors were encountered: