gpt4 book ai didi

javascript - Uncaught TypeError : not a function, 但函数存在

转载 作者:行者123 更新时间:2023-11-30 08:36:58 25 4
gpt4 key购买 nike

我的 js 文件中有以下内容:

var Field_File = function( _ )
{
var _objects = null;

var Init = function( instance, json )
{
// load _objects
}

var ImgLoad = function( elem, type )
{
//do things with _objects
}
}

现在在我的 PHP 中,我有:

<img id="file_uploader2_button" src="http://localhost/img/icons/up.png" onload="Field_File.ImgLoad(this, 'img');">

我已验证 JS 已正确加载到我的页面中,并且我通过同一个 PHP 页面调用其他 JS 函数没有任何问题:

body onload="Field_File.Init( '1', {$json});"

但我目前得到:

Uncaught TypeError: Field_File.ImgLoad is not a function

我在这次通话中是否漏掉了什么?

最佳答案

将模块(文字对象)的声明更改为

var Field_File = {
Init: function( instance, json ){
},
ImgLoad: function( elem, type ){
//do things
}
}

如果你想要一个范围来保护一些私有(private)变量,使用 IIFE :

var Field_File = (function(){
var _objects = null;
var Init = function( instance, json ){
// load _objects
}

var ImgLoad = function( elem, type ){
//do things with _objects
}

return {
Init:Init,
ImgLoad:ImgLoad
}
})();

可能有很多变体,所以重要的是要了解这里发生的事情,以便您可以根据自己的需要进行调整。

关于javascript - Uncaught TypeError : not a function, 但函数存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30487234/

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