Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数()存在,但原型函数()不存在';不存在。为什么?_Javascript_Function_Prototype Programming - Fatal编程技术网

Javascript 函数()存在,但原型函数()不存在';不存在。为什么?

Javascript 函数()存在,但原型函数()不存在';不存在。为什么?,javascript,function,prototype-programming,Javascript,Function,Prototype Programming,我正在创建一个名为ImageRotatorManager的JavaScript类来管理动态加载的幻灯片。通过xml加载图像时,我定义了以下函数: /* Loads configuration settings for the image rotator */ ImageRotatorManager.prototype.loadXML = function (callback) { jQuery.get("assets/image-rotator.xml", {}, function (x

我正在创建一个名为ImageRotatorManager的JavaScript类来管理动态加载的幻灯片。通过xml加载图像时,我定义了以下函数:

/* Loads configuration settings for the image rotator */
ImageRotatorManager.prototype.loadXML = function (callback) {
    jQuery.get("assets/image-rotator.xml", {}, function (xml) {
            parseXML(xml, callback); //the callback function                                             
    });
};

/* Loads configuration settings for the image rotator */
function parseXML(xml, callback) {
    //find every image and add the image to the '#slideshow' div
};
函数
parseXML(xml,回调)
调用成功

但是,如果我将parseXML()定义为
ImageRotatorManager.prototype.parseXML=function(xml,callback)
并使用
ImageRotatorManager.parseXML(xml,callback)调用此函数,我收到以下错误:

ImageRotatorManager.parseXML不是函数


为什么会出现这个错误?我使用此签名进行其他函数调用,它们工作正常。

您不能以这种方式调用
.parseXML()

您已经将它添加到原型中,因此必须在类的实例上调用它,而不是使用类名本身

试试这个:

ImageRotatorManager.prototype.loadXML = function (callback) {
    var self = this;
    jQuery.get("assets/image-rotator.xml", {}, function (xml) {
        self.parseXML(xml, callback); //the callback function
    });
};

能否将
parseXML()
直接分配给
ImageRotatorManager

ImageRotatorManager.parseXML = function(xml, callback) { ... };
并像在Java中调用静态方法一样调用它

ImageRotatorManager.parseXML(xml, callback);

我也尝试调用
this.parseXML()
,但这也不能解决我的问题。有什么想法吗?@Kyle:认真对待
这个
。它所指的内容取决于上下文。@Kyle请参阅
.get()
回调中的更新-
将发生更改。太棒了!你完全正确,
这个
在匿名函数中发生了变化。谢谢-@Kyle不客气。FWIW,
这个
在每个函数调用中都会发生变化。这可能会起作用,但这里真正的问题是如何在同一类的原型方法之间调用。嗨,David,谢谢你的建议。我更喜欢上面的答案,因为它消除了对静态函数的需要。