Hey right now I'm using jQuery and I have some global variables to hold a bit of preloaded ajax stuff (preloaded to make pages come up nice and fast):
嘿,现在我正在使用jQuery,我有一些全局变量来保存一些预加载的ajax东西(预加载以使页面变得漂亮和快速):
$.get("content.py?pageName=viewer", function(data)
{viewer = data;});
$.get("content.py?pageName=artists", function(data)
{artists = data;});
$.get("content.py?pageName=instores", function(data)
{instores = data;});
$.get("content.py?pageName=specs", function(data)
{specs = data;});
$.get("content.py?pageName=about", function(data)
{about = data;});
As you can see, we have a huge violation of the DRY principle, but... I don't really see a way to fix it... any ideas?
正如你所看到的,我们严重违反了DRY原则,但是......我真的没有办法解决它...任何想法?
maybe an array?
也许是阵列?
6 个解决方案
#1
5
You don't need eval()
or Function()
for this. An array, as you suspected, will do the job nicely:
您不需要eval()或Function()。您怀疑,数组可以很好地完成这项工作:
(function() // keep outer scope clean
{
// pages to load. Each name is used both for the request and the name
// of the property to store the result in (so keep them valid identifiers
// unless you want to use window['my funky page'] to retrieve them)
var pages = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i=0; i<pages.length; ++i)
{
// "this" refers to the outer scope; likely the window object.
// And will result in page contents being stored in global variables
// with the same names as the pages being loaded. We use the with({})
// construct to create a local scope for each callback with the
// appropriate context and page name.
with ({context: this, pageName: pages[i]})
$.get("content.py?pageName=" + pageName, function(data)
{context[pageName] = data;});
}
})(); // close scope, execute anonymous function
// at this point, viewer, artists, etc. are populated with page contents
// (assuming all requests completed successfully)
#2
6
Using the jQuery each method to iterate through an array of page names and then setting a global (in window scope) variable:
使用jQuery每个方法迭代一个页面名称数组,然后设置一个全局(在窗口范围内)变量:
jQuery.each(
["viewer", "artists", "instores", "specs", "about"],
function (page) {
$.get("content.py?pageName=" + page,
new Function("window[" + page + "] = arguments[0]"));
}
);
Update: Actually, you don't even need the "new Function":
更新:实际上,你甚至不需要“新功能”:
jQuery.each(
["viewer", "artists", "instores", "specs", "about"],
function (page) {
$.get("content.py?pageName=" + page, function () { window[page] = arguments[0]; });
}
);
#3
2
You can avoid eval using new Function:
您可以使用新函数避免使用eval:
var names = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i = 0; i < names.length; i++)
$.get("content.py?pageName=" + names[i], new Function('data', names[i] + ' = data;'));
It's not a lot better though tbh
虽然tbh并不是很好
#4
0
You can call only one time that page, and returning a json object instead of text
您只能调用该页面一次,并返回一个json对象而不是文本
{
viewer:'me',
artists:'you',
instores:'instores',
specs:'specs',
about:'about'
}
and eval that Since now you're calling N times your server, this slow down all, you should reconsider your logic!
并且评估从现在起你的服务器调用了N次,这一切都慢了,你应该重新考虑你的逻辑!
PS. as I write i saw the RoBorg answer, you see, when using new Function you are using eval under the hood, so if you want to use it go for it (in some browser is faster too)
PS。在我写的时候,我看到了RoBorg的答案,你看,当你使用新的功能时,你正在使用eval,所以如果你想使用它,那就去吧(在某些浏览器中也更快)
#5
0
This doesn't use eval, though it's a little more wordy.
这不使用eval,虽然它有点罗嗦。
function get_content(name){
$.get("content.py?pageName=" + name, function(data){ window[name] = data;});
}
var names = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i = 0; i < names.length; i++)
get_content(names[i]);
But one of the of answerers made a good point, you should probably try and combine all these requests into one otherwise your server will be hit 6 times for dynamic content on each request of the page.
但其中一个回答者提出了一个很好的观点,你应该尝试将所有这些请求合并为一个,否则你的服务器将在页面的每个请求上被动6次点击动态内容。
#6
0
Most of these proposed solutions avoid the use of eval. That practice is further reinforced in Doduglas Crockford's " Code Conventions for the JavaScript Programming Language" which says in part
大多数这些提出的解决方案都避免使用eval。 Doduglas Crockford的“JavaScript编程语言代码约定”部分地说明了这种做法
"eval is Evil
“eval是邪恶的
The eval function is the most misused feature of JavaScript. Avoid it.
eval函数是JavaScript最被误用的功能。躲开它。
eval has aliases. Do not use the Function constructor."
eval有别名。不要使用Function构造函数。“
#1
5
You don't need eval()
or Function()
for this. An array, as you suspected, will do the job nicely:
您不需要eval()或Function()。您怀疑,数组可以很好地完成这项工作:
(function() // keep outer scope clean
{
// pages to load. Each name is used both for the request and the name
// of the property to store the result in (so keep them valid identifiers
// unless you want to use window['my funky page'] to retrieve them)
var pages = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i=0; i<pages.length; ++i)
{
// "this" refers to the outer scope; likely the window object.
// And will result in page contents being stored in global variables
// with the same names as the pages being loaded. We use the with({})
// construct to create a local scope for each callback with the
// appropriate context and page name.
with ({context: this, pageName: pages[i]})
$.get("content.py?pageName=" + pageName, function(data)
{context[pageName] = data;});
}
})(); // close scope, execute anonymous function
// at this point, viewer, artists, etc. are populated with page contents
// (assuming all requests completed successfully)
#2
6
Using the jQuery each method to iterate through an array of page names and then setting a global (in window scope) variable:
使用jQuery每个方法迭代一个页面名称数组,然后设置一个全局(在窗口范围内)变量:
jQuery.each(
["viewer", "artists", "instores", "specs", "about"],
function (page) {
$.get("content.py?pageName=" + page,
new Function("window[" + page + "] = arguments[0]"));
}
);
Update: Actually, you don't even need the "new Function":
更新:实际上,你甚至不需要“新功能”:
jQuery.each(
["viewer", "artists", "instores", "specs", "about"],
function (page) {
$.get("content.py?pageName=" + page, function () { window[page] = arguments[0]; });
}
);
#3
2
You can avoid eval using new Function:
您可以使用新函数避免使用eval:
var names = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i = 0; i < names.length; i++)
$.get("content.py?pageName=" + names[i], new Function('data', names[i] + ' = data;'));
It's not a lot better though tbh
虽然tbh并不是很好
#4
0
You can call only one time that page, and returning a json object instead of text
您只能调用该页面一次,并返回一个json对象而不是文本
{
viewer:'me',
artists:'you',
instores:'instores',
specs:'specs',
about:'about'
}
and eval that Since now you're calling N times your server, this slow down all, you should reconsider your logic!
并且评估从现在起你的服务器调用了N次,这一切都慢了,你应该重新考虑你的逻辑!
PS. as I write i saw the RoBorg answer, you see, when using new Function you are using eval under the hood, so if you want to use it go for it (in some browser is faster too)
PS。在我写的时候,我看到了RoBorg的答案,你看,当你使用新的功能时,你正在使用eval,所以如果你想使用它,那就去吧(在某些浏览器中也更快)
#5
0
This doesn't use eval, though it's a little more wordy.
这不使用eval,虽然它有点罗嗦。
function get_content(name){
$.get("content.py?pageName=" + name, function(data){ window[name] = data;});
}
var names = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i = 0; i < names.length; i++)
get_content(names[i]);
But one of the of answerers made a good point, you should probably try and combine all these requests into one otherwise your server will be hit 6 times for dynamic content on each request of the page.
但其中一个回答者提出了一个很好的观点,你应该尝试将所有这些请求合并为一个,否则你的服务器将在页面的每个请求上被动6次点击动态内容。
#6
0
Most of these proposed solutions avoid the use of eval. That practice is further reinforced in Doduglas Crockford's " Code Conventions for the JavaScript Programming Language" which says in part
大多数这些提出的解决方案都避免使用eval。 Doduglas Crockford的“JavaScript编程语言代码约定”部分地说明了这种做法
"eval is Evil
“eval是邪恶的
The eval function is the most misused feature of JavaScript. Avoid it.
eval函数是JavaScript最被误用的功能。躲开它。
eval has aliases. Do not use the Function constructor."
eval有别名。不要使用Function构造函数。“