[英]Mini Refactoring Javascript Code
I want to optimize my Js code, at the moment i am rewriting the same function to launch a game in a popup. 我想优化我的Js代码,此刻我正在重写相同的功能以在弹出窗口中启动游戏。 The only difference between the functions (open_web_client, open_web_client_2) is the openPopup size 函数之间的唯一区别(open_web_client,open_web_client_2)是openPopup大小
I would like to use the same function for both games launched in the pop up, how can i use just a function for both in order to avoid repeating all the code? 我想对弹出窗口中启动的两个游戏使用相同的功能,如何才能仅对两者使用一个功能,以避免重复所有代码?
This is the code 这是代码
$(document).ready(function() {
web_client();
});
var web_client = function() {
var open_web_client = function(e) {
e.preventDefault();
if (!app.userIsLoggedIn()) {
app.showLoginPopup(translate.login_required_to_play_for_real);
} else {
if (Utils.analytics_enabled()) {
Utils.analytics_track_click('Play', $(this).attr("data-game-name"));
}
new GameWindow($(this).attr('href'), 'LOBBY').openPopup('1100x800');
}
}
var open_web_client_2 = function(e){
e.preventDefault();
if(!app.userIsLoggedIn()){
app.showLoginPopup(translate.login_required_to_play_for_real);
} else {
if(Utils.analytics_enabled()){
Utils.analytics_track_click('Play', $(this).attr("data-game-name"));
}
new GameWindow($(this).attr('href'), 'LOBBY').openPopup('1024x768');
}
}
if ($("a.ea_client").size() > 0) {
$('a.ea_client').on("click", open_web_client);
$('a.oneworks_client').on("click", open_web_client_2);
}
};
The only difference between the two functions is the value that is passed to openpopup
. 这两个函数之间的唯一区别是传递给openpopup
的值。 So create a common function and pass the dimensions to the event handler. 因此,创建一个通用函数并将维度传递给事件处理程序。
var open_web_client = function(e) {
e.preventDefault();
if (!app.userIsLoggedIn()) {
app.showLoginPopup(translate.login_required_to_play_for_real);
} else {
if (Utils.analytics_enabled()) {
Utils.analytics_track_click('Play', $(this).attr("data-game-name"));
}
//here the hardcoded value is replaced with e.data.dim
new GameWindow($(this).attr('href'), 'LOBBY').openPopup(e.data.dim);
}
};
then modify the handler code to pass the dimensions uniquely 然后修改处理程序代码以唯一地传递尺寸
$('a.ea_client').on("click",{dim:'1100x800'}, open_web_client);
$('a.oneworks_client').on("click",{dim:'1024x768'}, open_web_client);
arguments passed this way to handlers can be accessed through data
property present in the event object. 可以通过事件对象中存在的data
属性访问以这种方式传递给处理程序的参数。
The only difference between the functions (open_web_client, open_web_client_2) is the openPopup size 函数之间的唯一区别(open_web_client,open_web_client_2)是openPopup大小
That is basically begging to become a parameter of your function: 这基本上是乞讨成为你的函数的参数:
function open_web_client(e, size) {
e.preventDefault();
if (!app.userIsLoggedIn()) {
app.showLoginPopup(translate.login_required_to_play_for_real);
} else {
if (Utils.analytics_enabled()) {
Utils.analytics_track_click('Play', $(this).attr("data-game-name"));
}
new GameWindow($(this).attr('href'), 'LOBBY').openPopup(size);
}
}
$('a.ea_client').on("click", function(e) {
open_web_client(e, '1100x800');
});
$('a.oneworks_client').on("click", function(e) {
open_web_client(e, '1024x768');
});
A littlebit more advanced technique is to use a closure , with a function that creates the listener: 一点点更高级的技术是使用闭包 ,该闭包具有创建侦听器的功能:
function make_web_client_opener(size) {
return function open_web_client(e) {
e.preventDefault();
if (!app.userIsLoggedIn()) {
app.showLoginPopup(translate.login_required_to_play_for_real);
} else {
if (Utils.analytics_enabled()) {
Utils.analytics_track_click('Play', $(this).attr("data-game-name"));
}
new GameWindow($(this).attr('href'), 'LOBBY').openPopup(size);
}
};
}
$('a.ea_client').on("click", make_web_client_opener('1100x800'));
$('a.oneworks_client').on("click", make_web_client_opener('1024x768'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.