javascript - js函数表达式
高洛峰
高洛峰 2017-04-10 16:34:20
[JavaScript讨论组]
var a = function _a() {
            
        }
console.log(_a);
为什么这里   log的时候   会报错???
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(4)
伊谢尔伦
var a = function _a() {
    console.log(_a);
}
console.log(_a);

有名字的函数表达式的名字_a只在定义的函数体内有效,外面无效

大家讲道理

我们把有名字的匿名函数表达式中的函数体称为内联函数,而函数名 _a 只能在这个内联函数的作用域内使用。


ps:为什么叫内联函数,因为如果叫命名匿名函数表达式那特么太别扭了。

阿神

console.log(_a);
应该改为console.log(a);

PHPz

The BindingIdentifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the BindingIdentifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

根据ECMA-262的说明,函数表达式里面_a可以在函数体内使用,但是在函数外引用不到。

但是,如果写出这样:

function _a() {
}
console.log(_a);

就没有问题了。因为此时是函数声明,不是函数表达式了。

所以,此问题重点是分清函数声明函数表达式的区别。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号