gpt4 book ai didi

JavaScript 错误 : "is not a function"

转载 作者:行者123 更新时间:2023-12-03 02:24:46 25 4
gpt4 key购买 nike

看起来“$smth 不是一个函数”是 JavaScript 中一个非常常见的问题,但是在查看了相当多的线程之后,我仍然无法理解在我的情况下是什么导致了它。

我有一个自定义对象,定义为:

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();

然后在另一个脚本中,我尝试按以下方式使用此 API:

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;
}

getAPI() 的东西,看起来像这样:

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;
}

现在,API可能已找到,因为我没有收到“无法找到...”消息,代码继续尝试初始化它。但 firebug 告诉我 API.LMSInitialize 不是一个函数,如果我尝试使用 alert(Object.getOwnPropertyNames(API)); 调试它,它会给我一个空白警报。

我错过了什么?

最佳答案

有关调试此类问题的更多通用建议,MDN 有一篇很好的文章 TypeError: "x" is not a function :

It was attempted to call a value like a function, but the value is not actually a function. Some code expects you to provide a function, but that didn't happen.

Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function? For example, JavaScript objects have no map function, but JavaScript Array object do.

基本上,对象(js中的所有函数也是对象)并不存在于你认为存在的地方。这可能有许多原因包括(不是一个广泛的列表):

  • 缺少脚本库
  • 错别字
  • 该函数位于您当前无权访问的范围内,例如:

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
x.y();

  • 您的对象/函数没有您调用的函数:

var x = function(){
var y = function() {
alert('fired y');
}
};

//z is not a function error (as above) triggered
x.z();

关于JavaScript 错误 : "is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com