JavaScript error: "not a function"

It seems that "$ smth is not a function" is a very common JavaScript problem, but after looking at quite a few threads, I still cannot figure out what causes this in my case.

I have a custom object that is defined as:

function Scorm_API_12() { var Initialized = false; function LMSInitialize(param) { errorCode = "0"; if (param == "") { if (!Initialized) { Initialized = true; errorCode = "0"; return "true"; } else { errorCode = "101"; } } else { errorCode = "201"; } return "false"; } // some more functions, omitted. } var API = new Scorm_API_12(); 

Then in another script, I try to use this API as follows:

 var API = null; function ScormProcessInitialize(){ var result; API = getAPI(); if (API == null){ alert("ERROR - Could not establish a connection with the API."); return; } // and here the dreaded error pops up result = API.LMSInitialize(""); // more code, omitted initialized = true; } 

The getAPI () stuff is as follows:

 var findAPITries = 0; function findAPI(win) { // Check to see if the window (win) contains the API // if the window (win) does not contain the API and // the window (win) has a parent window and the parent window // is not the same as the window (win) while ( (win.API == null) && (win.parent != null) && (win.parent != win) ) { // increment the number of findAPITries findAPITries++; // Note: 7 is an arbitrary number, but should be more than sufficient if (findAPITries > 7) { alert("Error finding API -- too deeply nested."); return null; } // set the variable that represents the window being // being searched to be the parent of the current window // then search for the API again win = win.parent; } return win.API; } function getAPI() { // start by looking for the API in the current window var theAPI = findAPI(window); // if the API is null (could not be found in the current window) // and the current window has an opener window if ( (theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined") ) { // try to find the API in the current window s opener theAPI = findAPI(window.opener); } // if the API has not been found if (theAPI == null) { // Alert the user that the API Adapter could not be found alert("Unable to find an API adapter"); } return theAPI; } 

Now the API is probably found, because I do not get the message "Unable to find ...", the code is trying to initialize it. But firebug tells me the API.LMSInitialize is not a function , and if I try to debug it using alert(Object.getOwnPropertyNames(API)); he will give me an empty warning.

What am I missing?

+14
javascript javascript-objects
source share
4 answers

The LMSInitialize function LMSInitialize declared inside the Scorm_API_12 function. Thus, this can only be seen in the Scorm_API_12 .

If you want to use this function, for example API.LMSInitialize("") , declare the function Scorm_API_12 as follows:

 function Scorm_API_12() { var Initialized = false; this.LMSInitialize = function(param) { errorCode = "0"; if (param == "") { if (!Initialized) { Initialized = true; errorCode = "0"; return "true"; } else { errorCode = "101"; } } else { errorCode = "201"; } return "false"; } // some more functions, omitted. } var API = new Scorm_API_12(); 
+14
source share

For more general troubleshooting tips of this kind, MDN has a good TypeError article : "x" is not a function :

An attempt was made to call a value as a function, but in fact this value is not a function. Some code expects you to provide a function, but this did not happen.

Maybe there is a typo in the function name? Maybe the object you are calling the method for does not have this function? For example, JavaScript objects do not have a display function, and JavaScript objects have an Array.

In essence, an object (all functions in js are also objects) does not exist where you think it exists. This can be for many reasons, including (not an extensive list):

  • Missing script library
  • Typo
  • The function is located in an area that you currently do not have access to, for example:

 var x = function(){ var y = function() { alert('fired y'); } }; //the global scope can't access y because it is closed over in x and not exposed //y is not a function err triggered xy(); 
  • Your object / function does not have the function you are calling:

 var x = function(){ var y = function() { alert('fired y'); } }; //z is not a function error (as above) triggered xz(); 
+18
source share

I got this error because I had a circular addiction. Maybe this will help someone else.

+2
source share

I also hit this error. In my case, the main reason was related to asynchrony (during code base refactoring): the asynchronous function that creates the object that owns the function "not function" was not expected, and a subsequent attempt to call the function throws an error, for example below:

 const car = carFactory.getCar(); car.drive() //throws TypeError: drive is not a function 

The fix was:

 const car = await carFactory.getCar(); car.drive() 

Publication of this case will help anyone who encounters this error.

0
source share

All Articles