调用嵌套函数到同一个对象

Posted

技术标签:

【中文标题】调用嵌套函数到同一个对象【英文标题】:Call nested function to same object 【发布时间】:2014-03-04 13:24:03 【问题描述】:

我正在开发一个插件,在将对象添加到插件之后我想从外部调用一些事件到同一个对象,我该怎么做

(function ($, undefined) 

    jQuery.fn.pluginname = function (options) 
        var set = $.extend(
            opacity: 0.5             
        , options);

        //if(options.type)

        var cdiv = this;
        cdiv.css("opacity",set.opacity);

        function callthis()
        
            cdiv.css("display","none");
        
    
)(jQuery);


jQuery("#first").pluginname(opacity:0.5);

jQuery("#second").pluginname(opacity:0.5);

// call this function later

jQuery("#first").pluginname("callthis");

【问题讨论】:

【参考方案1】:

通常的方法是让你的插件接受“方法”作为字符串参数,例如:

jQuery("#first").pluginname("callThis");

在插件内部,您将该请求路由到函数。

通常,您还希望将最初使用的选项存储在闭包之外的某个地方; data 对此很有用。

你通常有一个"destroy" 方法来从元素中删除插件。

所以:

(function ($, undefined) 
    var methods = 
        init: function(options) 
            // Determine options
            var opts = $.extend(
                opacity: 0.5             
            , options);

            // Remember them
            this.data("pluginname", opts);

            // Initial stuff
            this.css("opacity", opts.opacity);
        ,

        callThis: function(opts) 
            // Use 'opts' if relevant
            this.css("display","none");
        ,

        destroy: function(opts) 
            this.removeData("pluginame");
            this.css("display", "").css("opacity", "");
        
    ;

    jQuery.fn.pluginname = function (options) 
        var method, args;

        // Method?
        if (typeof options === "string") 
            // Yes, grab the name
            method = options;

            // And arguments (we copy the arguments, then
            // replace the first with our options)
            args = Array.prototype.slice.call(arguments, 0);

            // Get our options from setup call
            args[0] = this.data("pluginname");
            if (!args[0]) 
                // There was no setup call, do setup with defaults
                methods.init.call(this);
                args[0] = this.data("pluginname");
            
        
        else 
            // Not a method call, use init
            method = "init";
            args = [options];
        

        // Do the call
        methods[method].apply(this, args);
    ;
)(jQuery);

Live Example | Source

【讨论】:

【参考方案2】:

试试这个:

(function ($, undefined) 

    jQuery.fn.pluginname = function (options) 
        var set = $.extend(
            opacity: 0.5             
        , options);

        //if(options.type)
        if(typeof options == function)
        return function callthis()
        
            cdiv.css("display","none");
        
         else
        return function()
        var cdiv = this;
        cdiv.css("opacity",set.opacity);
        
        


    
)(jQuery);

【讨论】:

以上是关于调用嵌套函数到同一个对象的主要内容,如果未能解决你的问题,请参考以下文章

函数对象函数嵌套名称空间与作用域闭包函数装饰器

从函数中退出以减少激活对象比递归或调用嵌套函数更好吗?

内部嵌套回调函数中调用未定义的对象

内部嵌套回调函数中调用未定义的对象

函数的装饰器

函数对象,名称空间,作用域,闭包函数