未捕获的类型错误:属性“fn”对象 [object DOMWindow] 的不是函数

发布于 2024-12-04 01:07:45 字数 192 浏览 0 评论 0原文

我知道如何修复此错误,但有人告诉我为什么会发生此错误的全面解释吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恋竹姑娘 2024-12-11 01:07:45

当在全局作用域中使用 var 关键字时,声明的变量将成为全局对象的属性。在 Web 浏览器中,全局对象是 window 对象,它本身是 DOMWindow() 的实例。因此,利用这些知识,我们可以将代码重写为如下所示:

window.fn = function () {
    return window.fn();
}();

去掉初始赋值,我们

(function () {
    return window.fn();
})();

......定义了一个匿名函数,其中调用 window.fn() 。但是,在这段代码运行时,window.fn 不是一个函数(并且永远不会是),因此会抛出异常,因为您试图调用它,即使它没有内部 [[Call]] 标志。

如果取消匿名函数的立即执行,那么 window.fn 将是一个函数:

var fn = function () {
    return fn();
}
fn(); //-> infinite loop

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 of DOMWindow(). So, using that knowledge we can rewrite your code to look like this:

window.fn = function () {
    return window.fn();
}();

Taking away the initial assignment, we have

(function () {
    return window.fn();
})();

...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:

var fn = function () {
    return fn();
}
fn(); //-> infinite loop
〃安静 2024-12-11 01:07:45
var fn = function () {
  return fn();
}();
  1. 上面的代码是一个变量语句。变量fn 已声明,其值设置为undefined(目前)。

  2. function () {}() 是一个 IIFE。 IIFE 是立即调用的函数。

  3. 该 IIFE 包含一个语句 - 一个 return 语句。由于 IIFE 已被调用,return 语句立即执行。

  4. 那个return语句包含这个表达式:fn() - 它是一个函数调用。但此时 fn 是什么?它是一个函数吗?不。它仍然是未定义。调用 undefined 值将引发错误。


我假设您可能想要实现这种模式:

var fn = (function () {

    var private = 0;

    function utility() { ... }

    return function () { ... };

})();

现在,fn 是一个对 private 变量和 utility private 具有独占访问权限的函数功能。

var fn = function () {
  return fn();
}();
  1. The above code is a variable statement. The variable fn is declared an it's value i set to undefined (for now).

  2. function () {}() is an IIFE. An IIFE is a function which is invoked immediately.

  3. That IIFE contains one statement - a return statement. Since the IIFE has been invoked, that return statement is executed immediately.

  4. That return statement contains this expression: fn() - it's a function invocation. But what is fn at this moment? Is it a function? No. It's still undefined. Invoking the undefined value will throw an error.


I assume you probably want to achieve the this pattern:

var fn = (function () {

    var private = 0;

    function utility() { ... }

    return function () { ... };

})();

Now, fn is a function which has exclusive access to the private variable and the utility private function.

向日葵 2024-12-11 01:07:45

你所说的是

  1. 执行函数 function () { return fn(); }
  2. 将返回值存储在名为 fn 的变量中

当然,问题在于,当 #1 发生时,fn 尚未定义,因此函数调用不能使用它; fn 不是函数。这相当于说“将X的值设置为X的值”,这是没有意义的。

除非您实际上试图返回调用 fn结果,这更没有意义。因此,即使它没有抱怨 fn 尚未定义,您仍然会得到无限递归,其中函数调用返回返回值的返回值......
正如 Shef 所说,这就是所谓的堆栈溢出错误

What you're saying is

  1. Execute the function function () { return fn(); }
  2. Store the returned value in a variable called 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 about fn 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

温柔嚣张 2024-12-11 01:07:45
var fn = function () {
  return this;
}();

你不能这样写,因为变量类型函数的定义没有在其他代码之前详细说明。除此之外,您尝试做的事情毫无意义。您可以创建如下函数:

function fn(){
    return fn();
}

fn();

但是您将遇到 stack_overflow 错误。

var fn = function () {
  return this;
}();

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:

function fn(){
    return fn();
}

fn();

But you are going to hit a stack_overflow error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文