JavaScript-为什么在函数表达式"undefined"中创建此函数声明? [英] JavaScript - Why is this function declaration created in a function expression "undefined"?

查看:39
本文介绍了JavaScript-为什么在函数表达式"undefined"中创建此函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想弄清楚这个函数表达式.

I'm just trying to get my head around this function expression.

似乎如果我创建一个似乎包含函数声明的函数表达式( p ),则函数声明 a()返回未定义.

It seems that if I create a function expression (p) that seems to contain a function declaration, the function declaration a() returns undefined.

var p;
p = function a() { return 'Hello' }

typeof p; // returns 'function'
typeof a; // returns 'undefined'

谁能解释为什么会这样?

Can anyone explain why this is the case?

也请让我知道我的术语是否也已停用.

And also please let me know if my terminology is off too.

推荐答案

它不是函数声明.这是一个恰好有名称的函数表达式.名称不会创建变量,但是您可以在对象上看到它

It isn't a function declaration. It is a function expression that happens to have a name. The name does not create a variable, but you can see it on the object

quentin@raston ~ $ node
> var p;
undefined
> p = function a() { return 'Hello' }
[Function: a]
> typeof p; // returns 'function'
'function'
> typeof a; // returns 'undefined'
'undefined'
> p
[Function: a]
> p.name
'a'
>

这篇关于JavaScript-为什么在函数表达式"undefined"中创建此函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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