最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Property 'fn' of object [object DOMWindow] is not a function - Stack Overflow

programmeradmin1浏览0评论

I know how to fix this error but does anyone tell me a prehensive explanation of why this error occurs?

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 prehensive explanation of why this error occurs?

var fn = function () {
  return fn();
}(); //uncaught TypeError: Property 'fn' of object [object DOMWindow] is not a function
Share edited Oct 5, 2011 at 10:32 AakashM 63.4k17 gold badges153 silver badges189 bronze badges asked Sep 11, 2011 at 10:38 antonjsantonjs 14.3k15 gold badges70 silver badges91 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

When you use the var keyword in the global scope, the declared variable bees 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
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.

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 plain 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

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.

发布评论

评论列表(0)

  1. 暂无评论