从 jQuery 函数中提取方法

发布于 2024-12-15 07:37:15 字数 3934 浏览 0 评论 0原文

我正在尝试从这个函数中提取一个方法:

addWidgetControls: function () {
        var iNettuts = this,
            $ = this.jQuery,
            settings = this.settings;

        $(settings.widgetSelector, $(settings.columns)).each(function () {
            var thisWidgetSettings = iNettuts.getWidgetSettings(this.id);
            if (thisWidgetSettings.removable) {
                $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
                    e.stopPropagation();
                }).click(function () {
                    $(this).parents(settings.widgetSelector).animate({
                        opacity: 0
                    }, function () {
                        $(this).wrap('<div/>').parent().slideUp(function () {
                            $(this).remove();
                        });
                    });

                    return false;
                }).appendTo($(settings.handleSelector, this));
            }

            if (thisWidgetSettings.collapsible) {
                $('<a href="#" class="collapse">COLLAPSE</a>').mousedown(function (e) {
                    e.stopPropagation();
                }).toggle(function () {
                    $(this).css({ backgroundPosition: '-38px 0' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).hide();
                    return false;
                }, function () {
                    $(this).css({ backgroundPosition: '' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).show();
                    return false;
                }).prependTo($(settings.handleSelector, this));
            }
        });
    },

我已经从这个函数中提取了代码,如下所示:

addWidgetControls: function () {
        var iNettuts = this,
            $ = this.jQuery,
            settings = this.settings,
            func = this.doWidget;

        $(settings.widgetSelector, $(settings.columns)).each(function () {
            func(this);
        });
    },

    doWidget: function (widg) {
        $ = jQuery;
        settings = iNettuts.settings;
        var thisWidgetSettings = iNettuts.getWidgetSettings(widg.id);
        if (thisWidgetSettings.removable) {
            $('<a href="#" class="remove"></a>').mousedown(function (e) {
                e.stopPropagation();
            }).click(function () {
                $(widg).parents(settings.widgetSelector).animate({
                    opacity: 0
                }, function () {
                    widg.wrap('<div/>').parent().slideUp(function () {
                        widg.remove();
                    });
                });

                return false;
            }).appendTo($(settings.handleSelector, widg));
        }

        if (thisWidgetSettings.collapsible) {
            $('<a href="#" class="collapse"></a>').mousedown(function (e) {
                e.stopPropagation();
            }).toggle(function () {
                $(widg).css({ backgroundPosition: '-38px 0' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).hide();
                return false;
            }, function () {
                $(widg).css({ backgroundPosition: '' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).show();
                return false;
            }).prependTo($(settings.handleSelector, widg));
        }
    },

我刚刚复制了函数代码,将 this 替换为 widg > 并添加 widg 作为参数。也许 $(widge)widg 之间存在差异。 但这不起作用。我缺少什么?有没有一个工具可以轻松做到这一点?谢谢。

PS:顺便说一下,这是从本教程中获取的。

I'm trying to extrac a method from this function:

addWidgetControls: function () {
        var iNettuts = this,
            $ = this.jQuery,
            settings = this.settings;

        $(settings.widgetSelector, $(settings.columns)).each(function () {
            var thisWidgetSettings = iNettuts.getWidgetSettings(this.id);
            if (thisWidgetSettings.removable) {
                $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
                    e.stopPropagation();
                }).click(function () {
                    $(this).parents(settings.widgetSelector).animate({
                        opacity: 0
                    }, function () {
                        $(this).wrap('<div/>').parent().slideUp(function () {
                            $(this).remove();
                        });
                    });

                    return false;
                }).appendTo($(settings.handleSelector, this));
            }

            if (thisWidgetSettings.collapsible) {
                $('<a href="#" class="collapse">COLLAPSE</a>').mousedown(function (e) {
                    e.stopPropagation();
                }).toggle(function () {
                    $(this).css({ backgroundPosition: '-38px 0' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).hide();
                    return false;
                }, function () {
                    $(this).css({ backgroundPosition: '' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).show();
                    return false;
                }).prependTo($(settings.handleSelector, this));
            }
        });
    },

I've extracted the code from this function like this:

addWidgetControls: function () {
        var iNettuts = this,
            $ = this.jQuery,
            settings = this.settings,
            func = this.doWidget;

        $(settings.widgetSelector, $(settings.columns)).each(function () {
            func(this);
        });
    },

    doWidget: function (widg) {
        $ = jQuery;
        settings = iNettuts.settings;
        var thisWidgetSettings = iNettuts.getWidgetSettings(widg.id);
        if (thisWidgetSettings.removable) {
            $('<a href="#" class="remove"></a>').mousedown(function (e) {
                e.stopPropagation();
            }).click(function () {
                $(widg).parents(settings.widgetSelector).animate({
                    opacity: 0
                }, function () {
                    widg.wrap('<div/>').parent().slideUp(function () {
                        widg.remove();
                    });
                });

                return false;
            }).appendTo($(settings.handleSelector, widg));
        }

        if (thisWidgetSettings.collapsible) {
            $('<a href="#" class="collapse"></a>').mousedown(function (e) {
                e.stopPropagation();
            }).toggle(function () {
                $(widg).css({ backgroundPosition: '-38px 0' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).hide();
                return false;
            }, function () {
                $(widg).css({ backgroundPosition: '' })
                        .parents(settings.widgetSelector)
                            .find(settings.contentSelector).show();
                return false;
            }).prependTo($(settings.handleSelector, widg));
        }
    },

I've just copied the function code, replaced this with widg and added widg as an argument. Maybe there is a difference between $(widge) and widg.
But it doesn't work. What am I missing? Is there a tool that can do this easily? Thanks.

PS: By the way, this is grabbed from this tutorial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一向站在原地 2024-12-22 07:37:15

如果我理解正确的话,您想要拆分 addWidgetControlsdoWidget

在您的实现中,您无权访问 addWidgetControls 中声明的局部变量,即 iNettuts$settings 两者之一,但您可以从上层访问它们。

var iNettuts = this, 是您的问题的最佳方式。我猜情况是,声明 iNettuts 超出了 addWidgetControls 的范围,因此如果它是完整的对象,也可以通过 doWidget 访问它。您可以在 doWidget 中本地重新声明 iNettuts,就像在 addWidgetControls 中一样。

顺便说一句,您不需要声明 redeclare。 doWiget 作为 addWidgetControls 中的 func,直接调用即可:iNettuts.doWidget(this);

if I understand correctly you want to split addWidgetControls and doWidget

In your implementation, you dont have access to local variables declared in addWidgetControls, namely iNettuts ($ and settings either, but you can access them from the upper level.

var iNettuts = this, is your problem. the best way in your situation is, i guess, to declare iNettuts out of the scope of addWidgetControls, so it's accessible by doWidget too. if it's the full object, you can redeclare iNettuts locally in doWidget exactly like in addWidgetControls.

On a side note, you don't need to declare redeclare doWiget as func in addWidgetControls, just call it directly like this: iNettuts.doWidget(this);

℡Ms空城旧梦 2024-12-22 07:37:15

问题解决了,真是有趣又好找!众所周知,this 指针有时会有所不同,在本例中,只有 3 个 this 实例应该被替换为 widg。所以这是最终的代码:

addWidgetControls: function () { 
    var iNettuts = this, 
        $ = this.jQuery, 
        settings = this.settings, 
        func = this.doWidget; 

    $(settings.widgetSelector, $(settings.columns)).each(function () { 
        func(this); 
    }); 
}, 

doWidget: function (wid) { 
    var $ = iNettuts.jQuery, 
        settings = iNettuts.settings; 

    var thisWidgetSettings = iNettuts.getWidgetSettings(wid.id); 
    if (thisWidgetSettings.removable) { 
        $('<a href="#" class="remove"></a>').mousedown(function (e) { 
            e.stopPropagation(); 
        }).click(function () { 
            $(this).parents(settings.widgetSelector).animate({ 
                opacity: 0 
            }, function () { 
                $(this).wrap('<div/>').parent().slideUp(function () { 
                    $(this).remove(); 
                }); 
            }); 

            return false; 
        }).appendTo($(settings.handleSelector, wid)); 
    } 

    if (thisWidgetSettings.collapsible) { 
        $('<a href="#" class="collapse"></a>').mousedown(function (e) { 
            e.stopPropagation(); 
        }).toggle(function () { 
            $(this).css({ backgroundPosition: '-38px 0' }) 
                    .parents(settings.widgetSelector) 
                        .find(settings.contentSelector).hide(); 
            return false; 
        }, function () { 
            $(this).css({ backgroundPosition: '' }) 
                    .parents(settings.widgetSelector) 
                        .find(settings.contentSelector).show(); 
            return false; 
        }).prependTo($(settings.handleSelector, wid)); 
    } 
}, 

The problem is solved and it was really interesting and easy to find! The this pointer, as we all know, is different from time to time, and in this case, there were only 3 instances of this that should've been replaced with widg. So here is the final code:

addWidgetControls: function () { 
    var iNettuts = this, 
        $ = this.jQuery, 
        settings = this.settings, 
        func = this.doWidget; 

    $(settings.widgetSelector, $(settings.columns)).each(function () { 
        func(this); 
    }); 
}, 

doWidget: function (wid) { 
    var $ = iNettuts.jQuery, 
        settings = iNettuts.settings; 

    var thisWidgetSettings = iNettuts.getWidgetSettings(wid.id); 
    if (thisWidgetSettings.removable) { 
        $('<a href="#" class="remove"></a>').mousedown(function (e) { 
            e.stopPropagation(); 
        }).click(function () { 
            $(this).parents(settings.widgetSelector).animate({ 
                opacity: 0 
            }, function () { 
                $(this).wrap('<div/>').parent().slideUp(function () { 
                    $(this).remove(); 
                }); 
            }); 

            return false; 
        }).appendTo($(settings.handleSelector, wid)); 
    } 

    if (thisWidgetSettings.collapsible) { 
        $('<a href="#" class="collapse"></a>').mousedown(function (e) { 
            e.stopPropagation(); 
        }).toggle(function () { 
            $(this).css({ backgroundPosition: '-38px 0' }) 
                    .parents(settings.widgetSelector) 
                        .find(settings.contentSelector).hide(); 
            return false; 
        }, function () { 
            $(this).css({ backgroundPosition: '' }) 
                    .parents(settings.widgetSelector) 
                        .find(settings.contentSelector).show(); 
            return false; 
        }).prependTo($(settings.handleSelector, wid)); 
    } 
}, 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文