问题描述
在以下示例中,我将一个数字添加到 typeof 函数,但结果是 1undefined.为什么?
In the following example I am adding a number to the typeof function, but result is 1undefined. why?
var y = 1;
if (function f(){}) {
y += typeof f;
}
console.log(y);
预期结果:
1function
实际结果:
1undefined
谁能帮我理解结果不是 1function 吗?我知道如果块没有自己的范围,所以不知道为什么在圆括号中该函数在圆括号之外不可见.
Can someone help me out understanding how the result is not 1function? I know that if block does not have its own scope, so no sense why in round braces the function is not visible outside the round braces.
推荐答案
那里的函数被评估为 函数表达式,因为它在 if 语句中.因此,它不会被提升,也不会在除了它自己的函数体之外的任何地方可见.if 语句的内部需要一个值,因此它被视为一个表达式.
The function there is being evaluated as a function expression because it's in an if statement. Thus, it doesn't get hoisted, nor is it visible anywhere other than in its own function body. The inside of the if statement is expecting a value, so it's treated as an expression.
只有函数声明被提升并在其块中的任何位置可见.
Only function declarations get hoisted and become visible anywhere in their block.
这篇关于函数的类型没有返回正确的值但未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)