Javascript 我的jQuery嵌套函数无法返回值

Javascript 我的jQuery嵌套函数无法返回值,javascript,jquery,Javascript,Jquery,我有一个jQuery插件,其中一个嵌套函数存储在数据对象上,以便以后调用它。我的问题是,当我调用这个函数时,它似乎不能返回一个简单的值,它要么返回未定义的对象,要么返回插件所附加的html对象 var findTheObject = function(self,needle) { var haystack = self.data('galleryData'); console.log('haystack is:'); console.lo

我有一个jQuery插件,其中一个嵌套函数存储在数据对象上,以便以后调用它。我的问题是,当我调用这个函数时,它似乎不能返回一个简单的值,它要么返回未定义的对象,要么返回插件所附加的html对象

    var findTheObject = function(self,needle) {

        var haystack = self.data('galleryData');
        console.log('haystack is:');
        console.log(haystack);

        console.log('needle is:');
        console.log(needle);

        for (i = 0; i < haystack.length; ++i) {
            console.log('looking for needle...');
            if (haystack[i]['fileId'] == needle) {
                console.log('found needle:');
                console.log(haystack[i]);
                return haystack[i];
            }
        }

        return 'found no match';
    };
    this.data('findTheObject',findTheObject); //make callable later on through the data array.
引用Satpal:

像self.data“findTheObject”一样使用它

在语句self.datafindTheObjectself,needle中,首先调用全局函数findTheObject,然后调用它返回的任何内容,假设是字符串abc,那么它正在查找self.data'abc'

结束报价


当我的问题被回答时,我喜欢核对一下。此答案的可信度为,但由于他选择不将其作为答案发布,我将总结他的答案。如果您发布答案,我将检查答案,并删除我的答案,以获得可信度。

像自我一样使用它。数据“findTheObject”自我,谢谢!很好用!好奇-我调用它的方式发生了什么-函数运行得很好,我可以读取我的console.log调试语句,唯一不起作用的是返回值…在语句self.datafindTheObjectself,needle,首先调用全局函数findTheObject,然后不管它返回什么,假设是字符串abc,那么它正在查找self.data'abc',如果您将解决方案作为答案发布,我可以检查它底线是,不要为回答这样的问题感到难过:
        if (self.data("activeFolder") != null) {
            var needle = self.data("activeFolder");
            var haystack = self.data('galleryData');

            current = null;
            //find the object corresponding to data("activeFolder")

            current = self.data(findTheObject(self,needle));

            console.log('returned object is:');
            console.log(current);