未捕获的类型错误:属性“fn”对象 [object DOMWindow] 的不是函数
我知道如何修复此错误,但有人告诉我为什么会发生此错误的全面解释吗?
var fn = function () {
return fn();
}(); //uncaught TypeError: Property 'fn' of object [object DOMWindow] is not a function
I know how to fix this error but does anyone tell me a comprehensive explanation of why this error occurs?
var fn = function () {
return fn();
}(); //uncaught TypeError: Property 'fn' of object [object DOMWindow] is not a function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当在全局作用域中使用 var 关键字时,声明的变量将成为全局对象的属性。在 Web 浏览器中,全局对象是
window
对象,它本身是DOMWindow()
的实例。因此,利用这些知识,我们可以将代码重写为如下所示:去掉初始赋值,我们
......定义了一个匿名函数,其中调用
window.fn()
。但是,在这段代码运行时,window.fn
不是一个函数(并且永远不会是),因此会抛出异常,因为您试图调用它,即使它没有内部[[Call]]
标志。如果取消匿名函数的立即执行,那么
window.fn
将是一个函数:When you use the var keyword in the global scope, the declared variable becomes a property of the global object. In a web browser, the global object is the
window
object, which itself is an instance ofDOMWindow()
. So, using that knowledge we can rewrite your code to look like this:Taking away the initial assignment, we have
...which defines an anonymous function wherein
window.fn()
is called. However, at the point this code runs,window.fn
is not a function (and never will be), so an exception is thrown because you're trying to invoke it even though it doesn't have an internal[[Call]]
flag.If you take away the immediate execution of the anonymous function, then
window.fn
would be a function:上面的代码是一个变量语句。变量
fn
已声明,其值设置为undefined
(目前)。function () {}()
是一个 IIFE。 IIFE 是立即调用的函数。该 IIFE 包含一个语句 - 一个
return
语句。由于 IIFE 已被调用,return
语句立即执行。那个
return
语句包含这个表达式:fn()
- 它是一个函数调用。但此时fn
是什么?它是一个函数吗?不。它仍然是未定义
。调用undefined
值将引发错误。我假设您可能想要实现这种模式:
现在,
fn
是一个对private
变量和utility
private 具有独占访问权限的函数功能。The above code is a variable statement. The variable
fn
is declared an it's value i set toundefined
(for now).function () {}()
is an IIFE. An IIFE is a function which is invoked immediately.That IIFE contains one statement - a
return
statement. Since the IIFE has been invoked, thatreturn
statement is executed immediately.That
return
statement contains this expression:fn()
- it's a function invocation. But what isfn
at this moment? Is it a function? No. It's stillundefined
. Invoking theundefined
value will throw an error.I assume you probably want to achieve the this pattern:
Now,
fn
is a function which has exclusive access to theprivate
variable and theutility
private function.你所说的是
function () { return fn(); }
fn
的变量中当然,问题在于,当 #1 发生时,
fn
尚未定义,因此函数调用不能使用它;fn
不是函数。这相当于说“将X的值设置为X的值”,这是没有意义的。除非您实际上试图返回调用
fn
的结果,这更没有意义。因此,即使它没有抱怨fn
尚未定义,您仍然会得到无限递归,其中函数调用返回返回值的返回值......正如 Shef 所说,这就是所谓的堆栈溢出错误
What you're saying is
function () { return fn(); }
fn
Trouble is, of course, that when #1 is happening,
fn
hasn't been defined yet, so the function call can't use it;fn
is not function. It's the same as saying "set the value of X to the value of X", which doesn't make sense.Except you're actually trying to return the result of calling
fn
, which makes even less sense. So even if it somehow didn't complain aboutfn
not being defined yet, you'd still get an infinite recursion where the function call returns the returned value of the returned value of the returned value …As Shef says, that's what's called a stack overflow error
你不能这样写,因为变量类型函数的定义没有在其他代码之前详细说明。除此之外,您尝试做的事情毫无意义。您可以创建如下函数:
但是您将遇到
stack_overflow
错误。You can't write that, because variable type function definition is not elaborated before the other code. Besides what you are trying to do makes no sense. You could create the function like:
But you are going to hit a
stack_overflow
error.