从 jQuery 函数中提取方法
我正在尝试从这个函数中提取一个方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您想要拆分
addWidgetControls
和doWidget
在您的实现中,您无权访问
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
anddoWidget
In your implementation, you dont have access to local variables declared in
addWidgetControls
, namelyiNettuts
($
andsettings
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 declareiNettuts
out of the scope ofaddWidgetControls
, so it's accessible bydoWidget
too. if it's the full object, you can redeclareiNettuts
locally indoWidget
exactly like inaddWidgetControls
.On a side note, you don't need to declare redeclare doWiget as
func
inaddWidgetControls
, just call it directly like this:iNettuts.doWidget(this);
问题解决了,真是有趣又好找!众所周知,this 指针有时会有所不同,在本例中,只有 3 个 this 实例应该被替换为 widg。所以这是最终的代码:
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: