Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
Jquery 私有方法作为字符串返回_Jquery_Plugins_Methods - Fatal编程技术网

Jquery 私有方法作为字符串返回

Jquery 私有方法作为字符串返回,jquery,plugins,methods,Jquery,Plugins,Methods,我正在开发一个简单的jquery插件,我在设置方法结构时遇到了困难。有人能告诉我吗。我正在使用官方Jquery编写文档中描述的插件结构 我遇到的问题是,在调用私有函数_generateID时,该函数实际上返回函数文本(function(){return this..),而不是“hi” (function( $ ){ var methods = { init : function( options ) { return this.each(func

我正在开发一个简单的jquery插件,我在设置方法结构时遇到了困难。有人能告诉我吗。我正在使用官方Jquery编写文档中描述的插件结构

我遇到的问题是,在调用私有函数_generateID时,该函数实际上返回函数文本(function(){return this..),而不是“hi”

(function( $ ){

    var methods = {
        init : function( options ) {
            return this.each(function() {

            });
        },

        _generateID : function() {
            return this.each(function() {
                return 'hi';
            });
        },

        create : function( options ) {
            return this.each(function() {
                var settings = {
                    'id' : methods._generateID,
                };
                if ( options ) { $.extend( settings, options ); }
                $('<div>', {
                    id : settings.id,
                }).appendTo(this);              
            });
        },

        destroy : function( id ) {
            return this.each(function(){
                $(window).unbind('#'+id);
                $('#'+id).remove();
            });
        }
    };

    $.fn.workzone = function( method ) {
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.workzone' );
        }    
    };

})( jQuery );
(函数($){
var方法={
初始化:函数(选项){
返回此值。每个(函数(){
});
},
_generateID:function(){
返回此值。每个(函数(){
返回“hi”;
});
},
创建:函数(选项){
返回此值。每个(函数(){
变量设置={
“id”:方法。\u generateID,
};
如果(选项){$.extend(设置,选项);}
$('', {
id:settings.id,
}).附于(本);
});
},
销毁:功能(id){
返回此值。每个(函数(){
$(窗口)。解除绑定(“#”+id);
$('#'+id).remove();
});
}
};
$.fn.workzone=函数(方法){
if(方法[方法]){
返回方法[method].apply(this,Array.prototype.slice.call(arguments,1));
}else if(typeof方法=='object'| |!方法){
return methods.init.apply(这是参数);
}否则{
$.error('Method'+Method+'在jQuery.workzone上不存在);
}    
};
})(jQuery);

必须用括号调用函数
方法。
必须用括号调用函数
方法。

必须用括号调用函数。
在哪里调用函数?我只看到:

var settings = {
   'id' : methods._generateID,
};
也许你的意思是:

var settings = {
   'id' : methods._generateID(),
};

函数调用的形式为
return\u value=Function\u name(arg\u list);

在哪里调用函数?我只看到:

var settings = {
   'id' : methods._generateID,
};
也许你的意思是:

var settings = {
   'id' : methods._generateID(),
};

函数调用的形式为
return\u value=Function\u name(arg\u list);

。在插件内或从外部调用

Headshota用于调用插件中的方法

$('div').pluginName('_generateID', arg0, arg1, ...); //Way to call a plugin method from outside out side plugin and pass arguements.

请注意,将方法名作为第一个字符串参数传递,然后是其他参数。

从您调用的位置。在插件内或从外部调用

Headshota用于调用插件中的方法

$('div').pluginName('_generateID', arg0, arg1, ...); //Way to call a plugin method from outside out side plugin and pass arguements.

请注意,将方法名称作为第一个字符串参数传递,然后再传递其他参数。

BTW它并没有返回“字符串”,而是在输出
settings.id
的某个地方,输出机制正在可视化函数引用。顺便说一句,它没有返回“字符串”,但在您输出
设置.id
的某个地方,输出机制正在可视化函数引用。谢谢。这确实有效。但我也意识到了另一个问题。函数正在返回此值。每次我只需返回我的id。我现在都已解决了:)谢谢谢谢谢谢。这确实有效。但我还发现了另一个问题。函数正在返回这个。每次当我需要简单地返回我的Id时,我都得到了解决:)谢谢