JavaScript错误:";不是一个函数;
看起来“$smth不是一个函数”是JavaScript的一个非常常见的问题,但是在浏览了很多线程之后,我仍然无法理解是什么导致了我的情况 我有一个自定义对象,定义为:JavaScript错误:";不是一个函数;,javascript,javascript-objects,Javascript,Javascript Objects,看起来“$smth不是一个函数”是JavaScript的一个非常常见的问题,但是在浏览了很多线程之后,我仍然无法理解是什么导致了我的情况 我有一个自定义对象,定义为: function Scorm_API_12() { var Initialized = false; function LMSInitialize(param) { errorCode = "0"; if (param == "") { if (!Initialized) {
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;
}
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();
现在,可能找到了API,因为我没有收到“无法找到…”消息,代码继续尝试初始化它。但是firebug告诉我API.LMSInitialize不是一个函数
,如果我尝试用警报(Object.getOwnPropertyNames(API))调试它代码>,它会给我一个空白警报
我缺少什么?您的LMSInitialize
函数在Scorm\u API\u 12
函数中声明。因此,它只能在Scorm_API_12
函数的作用域中看到
如果您想像API.LMSInitialize(“”)那样使用此函数,请像下面这样声明Scorm_API_12
函数:
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;
}
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();
有关调试此类问题的更多通用建议,MDN有一篇好文章:
试图像函数一样调用一个值,但该值不可用
实际上是一个函数。有些代码希望您提供函数,但是
那没有发生
也许函数名中有输入错误?也许你就是这样的对象
调用上的方法没有此函数?例如
JavaScript对象没有映射函数,但是JavaScript数组对象
做
基本上,对象(js中的所有函数也是对象)并不存在于您认为它存在的地方。原因可能有很多,包括(不是一个广泛的列表):
- 缺少脚本库
- 打字错误
- 该功能在您当前无权访问的范围内,例如:
var x=function(){
var y=函数(){
警报('y');
}
};
//全局作用域无法访问y,因为它在x中已关闭且未公开
//y不是已触发的函数错误
x、 y()代码>我也遇到了这个错误。在我的例子中,根本原因是异步相关的(在代码库重构期间):没有等待生成“nota function”函数所属对象的异步函数,随后调用该函数的尝试会抛出错误,示例如下:
const car = carFactory.getCar();
car.drive() //throws TypeError: drive is not a function
解决办法是:
const car = await carFactory.getCar();
car.drive()
发布此消息有助于其他任何面临此错误的人。我在使用JSON.parse和JSON.stringify()复制类对象时收到此错误,该函数删除了以下函数:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log('area of square: ', square.calcArea());
const squareCopy = JSON.parse(JSON.stringify(square));
// Will throw an exception since calcArea() is no longer function
console.log('area of square copy: ', squareCopy.calcArea());
除了上面流行的答案之外,如果您正在使用服务或助手功能文件,并对稍后将在项目中导入的功能执行导出
确保要导入的函数名与从服务、帮助程序或utils文件导出的函数的确切名称匹配,并且该函数实际存在于正确的文件中!我被这个错误缠住了,调试了几个小时,直到我发现这个错误才有所进展。当你在API=getAPI()之后执行console.log(API)
时,你会得到什么代码>?请告诉我初始化后要执行的操作。我遇到了这个错误,因为我有一个循环依赖项。也许这会对其他人有所帮助。我想补充一点:将局部变量命名为函数,因此当调用showOrderForm()
时,showOrderForm
的类型是布尔值。