如果你不能用你给它们的名字真正引用它们,那么命名函数表达式有什么意义呢? [英] What's the point of naming function expressions if you can't really reference them by the names you give them?

查看:86
本文介绍了如果你不能用你给它们的名字真正引用它们,那么命名函数表达式有什么意义呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你不能用你给它们的名字真正引用它们,那么命名函数表达式有什么意义?

What's the point of naming function expressions if you can't really reference them by the names you give them?

var f = function g() {
    console.log("test");
};

g(); // ReferenceError: g is not defined


推荐答案

哦,但是您可以通过该名称引用它们。这些名称只存在 in 函数的范围。

Oh, but you can reference them by that name. Those names just only exist inside the scope of the function.

var f = function g() {
    // In here, you can use `g` (or `f`) to reference the function
    return typeof g;
};

console.log( typeof g );
// It only exists as `f` here
console.log( f() );

DOCS: https:// developer。 mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function#Named_function_expression

这篇关于如果你不能用你给它们的名字真正引用它们,那么命名函数表达式有什么意义呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆