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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
windowobject, 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.fnis 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.fnwould be a function: