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

Day10:写出执行结果,并解释原因 #46

Open
Genzhen opened this issue Jun 22, 2020 · 7 comments
Open

Day10:写出执行结果,并解释原因 #46

Genzhen opened this issue Jun 22, 2020 · 7 comments

Comments

@Genzhen
Copy link
Collaborator

Genzhen commented Jun 22, 2020

var x=1;
if(function f(){}){
    x += typeof f;
}
console.log(x)
// 写出执行结果,并解释原因

每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案
欢迎大家在下方发表自己的优质见解
二维码加载失败可点击 小程序二维码

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。

@Genzhen
Copy link
Collaborator Author

Genzhen commented Jun 22, 2020

答案
1 undefined

解析
条件判断为假的情况有:0,false,'',null,undefined,未定义对象。函数声明写在运算符中,其为true,但放在运算符中的函数声明在执行阶段是找不到的。另外,对未声明的变量执行typeOf不会报错,会返回undefined

@Genzhen Genzhen closed this as completed Jul 20, 2020
@Genzhen Genzhen reopened this Jul 29, 2020
@yanwenxi
Copy link

结果应该是NaN吧,1+undefined=NaN

@Genzhen
Copy link
Collaborator Author

Genzhen commented Feb 22, 2021

结果应该是NaN吧,1+undefined=NaN

typeof 返回的是字符串哦

@liuyuan22
Copy link

liuyuan22 commented Mar 17, 2021

放在运算符中的函数声明在执行阶段是找不到的

理解这个意思,但是这种行为是写在规范里的么?找了一圈没找到,有没有什么出处?

@DakerHub
Copy link

DakerHub commented May 6, 2021

没有10年脑血栓应该写不出这种代码

@wind8866
Copy link

函数声明写在运算符中,其为true,但放在运算符中的函数声明在执行阶段是找不到的。

@Genzhen 不太理解这句话的意思。

我测试在严格模式下,chrome 执行结果为1function
个人理解,函数声明会被提升到所有代码执行前,所以在整个代码任何地方都能访问到f。

@luzhaopan
Copy link

1object
image

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

No branches or pull requests

6 participants