帮助重构功能...... [英] Help refactoring functions...

查看:64
本文介绍了帮助重构功能......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有:


我有以下(丑陋)代码:


功能预载()

{

if(cr_series.length> 1)preloadCR();

if(ct_series.length> 1)preloadCT();

if(mr_series.length> 1)preloadMR();

if(us_series.length> 1)preloadUS();

if(xr_series.length> ; 1)preloadXR();

}


函数preloadCR()

{

cr_images = new Array();

for(var loop = 0; loop< =(cr_series.length-2); loop ++)

{

cr_images [loop] = new Array();


for(var i = 0; i< 4; i ++)

{

cr_images [0] [i] = new Image();

cr_images [0] [i] .src =" images / cr" +(循环+ 1)+_ +(i + 1)+" .jpg";

}

}

}


function preloadCT()

{

ct_images = new Array();

for(var loop = 0; loop< =( ct_series.length-2); loop ++)

{

ct_images [loop] = new Array();


for( var i = 0; i< 4; i ++)

{

ct_images [0] [i] = new Image();

ct_images [0] [i] .src =" images / ct" +(循环+ 1)+_ +(i + 1)+" .jpg";

}

}

}

当你可以看到,两个预加载函数(以及其他我没有在这里粘贴的b $ b)除了数组的名称之外几乎完全相同

创建和使用。


我是初学者,在写一个通用函数时遇到了麻烦,

可以做同样的事情,但是用一个参数调用:


if(cr_series.length> 1)preload(''cr'');


我不知道如何通过在一个表达式的''左''

一侧使用它来动态创建一个变量名等等。

任何人都可以指出我在右边方向?


谢谢,

cjl

解决方案

cjl写道:

嘿所有:

我有以下(丑陋的)代码:

函数preload()
{
if(cr_series.length> 1 )preloadCR();
if(ct_series.length> 1)preloadCT();
if(mr_series.length> 1)preloadMR();
if(us_series.length> 1)preloadUS();
if(xr_series.length> 1)preloadXR();
}
函数preloadCR()
{
cr_images = new Array();
for(var loop = 0; loop< =(cr_series.length-2);循环++)
{
cr_images [loop] = new Array();

for(var i = 0; i< 4; i ++)
{
cr_images [0] [i] = new Image();
cr_images [0] [i] .src =" images / cr" +(循环+ 1)+_ +(i + 1)+" .jpg";
}
}


函数preloadCT()
{
ct_images = new Array();
for(var loop = 0; loop< =(ct_series.length-2); loop ++)
{
ct_images [loop] = new Array();

for(var i = 0; i< 4; i ++)
{
ct_images [0] [i] = new Image();
ct_images [0 ] [i] .src =" images / ct" +(循环+ 1)+_ +(i + 1)+" .jpg";
}
}
}

如您所见,两个预加载函数(以及其他函数)除了创建和使用的阵列的名称之外,它们都没有相同的粘贴。

我是初学者,在编写通用文章时遇到了麻烦功能可以做同样的事情,但是用一个参数调用:

if(cr_series.length> 1)preload(''cr'');


这是正确的想法...


发布代码时,请使用2或4个空格的缩进,而不是制表符,以及手动

换行约70个字符以防止自动换行。对于那些会更容易回复的人来说,它会让你的生活更加美好。

我不知道如何接受传递的paramated并在''left'上使用它
表达式的一侧动态创建一个变量名称等....
任何人都能指出我正确的方向吗?




创建数组的函数,然后返回对它的引用到原始函数的
(我假设你想要使这些全局化)。你是否想要将图像加载到''cr_series'','ct_series'等等或新的

变量?

我会假设您在其他地方创建了''cr_series''等全局变量

并且您想要将图像加载到其中。看来

您传递的参数是存储图像的目录名,因此我已将其作为字符串传递。


我对''+1''位有点困惑。制作

所有零索引的内容要容易得多,但是如果你不能改变它我想你只是

必须使用它(但它使代码更加钝,更难以维持b $ b。

函数preload(){

if(cr_series.length> 1) cr_series = loadArray(''cr'');

if(ct_series.length> 1)ct_series = loadArray(''ct'');

//和等等...

}


函数loadArray(parm){

var A = [];

for(var loop = 0,len =(cr_series.length-2); loop< = len; loop ++){

A [loop] = [];


for(var i = 0; i< 4; i ++){

A [0] [i] = new Image();

A [0] [i] .src =''images /''+ parm +(loop + 1)

+''_''+(i + 1)+''.jpg' ';

}

}

返回A;

}


A是''loadArray()''函数的本地。它创建数组,然后

返回对调用函数的引用(preload)。因为

引用仍然存在,所以数组保持活动状态。


这是loadArray函数的更简洁版本:


function loadArray(d){

var A = [];

var j,i = cr_series.length; //没有'' - 2''...见下文


而( - i){

A [i] = [];

j = 5; //不是4 ...见下文


while( - j){

A [i] [j] = new Image();

A [i] [j] .src =''images /''+ d + i +''_''+ j +''.jpg'';

}

}

返回A;

}


我用过前-decrement运算符(--i)这样我将在while循环中将值

(无论它是设置为-1)降低到1(当它为
时)
在执行循环内容之前退出*时为零。同样适用于j。

保存所有+/- 1个东西。


您可能也希望将j的值作为参数传递,硬编码'5''
在loadArray函数中的
减少了它的可重用性。


当然未经测试,但它应该可以工作(好吧,也许稍微调试

需要......)。

-

Rob


Rob:


感谢您的快速回复。我会按照你的建议将来发布代码




您写道:

我会认为你创建''cr_series''等作为全局变量
在其他地方,并且您想要将图像加载到它们中。您传递的参数似乎是存储图像的目录名称,因此我已将其作为字符串传递。


你猜对了。有一个名为''images''的子目录。其中

用户将放置相关图像,遵守命名

惯例。如果有两个系列的CR图像和一个

系列CT图像,该目录可能包含cr1_1.jpg,cr1_2.jpg,

cr1_3.jpg ,cr1_4.jpg,cr2_1.jpg,cr2_2.jpg,ct1_1.jpg,ct1_2.jpg,

ct1_3.jpg,ct1_4.jpg,ct1_5.jpg"等等,你得到了图片。然后

用户在一个单独的js文件中编辑数组,这样在这个例子中

会有:


cr_series = [4,2,0];

ct_series = [5,0];

我对''+1''位有点困惑。使一切零索引变得容易得多,但是如果你不能改变它我想你只需要使用它(但它会使代码更加钝化而且更多<难以维持)。


有些用户不会理解零索引,并且图像命名

约定会受到影响。

功能preload(){
if(cr_series.length> 1)cr_series = loadArray(''cr'');
if(ct_series.length> 1)ct_series = loadArray(''ct'' );
//等...

函数loadArray(parm){
var A = [];
for(var loop = 0,len =(cr_series.length-2); loop< = len; loop ++){
A [loop] = [];

for(var i = 0; i< ; 4; i ++){
A [0] [i] =新图像();
A [0] [i] .src =''images /''+ parm +(loop + 1 )
+''_''+(i + 1)+''.jpg'';
}
}
返回A;
}



我认为此代码存在问题,这正是我在原帖中要问的问题。

问题。


你的代码具有cr_series.length硬编码进入loadArray

函数。这个数组实际上需要改变,这取决于传递给函数的

参数。如果我想预加载CR图像,那么

cr_series.length很好,但是你的例子不适用于CT

图像。


让我试着解释一下我想写的泛型函数。如果

传递的参数是cr,那么它应该像我的原始代码一样创建一个

(多维)数组cr_images。如果我通过

" ct"然后它应该创建一个(多维)数组ct_images。

这些数组随后将在代码中被访问。


类似于:

preload(foo)

{

''foo''_ array = code goes here;

}


这可能吗?


再次感谢,

cjl


嘿伙计们;


另一个想法刚刚发生在我身上......

是eval()我想要动态创建变量等等。$


-CJL


Hey all:

I have the following (ugly) code:

function preload()
{
if (cr_series.length >1) preloadCR();
if (ct_series.length >1) preloadCT();
if (mr_series.length >1) preloadMR();
if (us_series.length >1) preloadUS();
if (xr_series.length >1) preloadXR();
}

function preloadCR()
{
cr_images = new Array();
for (var loop = 0; loop <= (cr_series.length-2); loop++)
{
cr_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
cr_images[0][i] = new Image();
cr_images[0][i].src = "images/cr" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}

function preloadCT()
{
ct_images = new Array();
for (var loop = 0; loop <= (ct_series.length-2); loop++)
{
ct_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
ct_images[0][i] = new Image();
ct_images[0][i].src = "images/ct" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}
As you can see, the two preload functions (and the others I didn''t
paste here) are all but identical, except for the names of the arrays
created and used.

I am a beginner, and having trouble writing a generic function that
could do the same thing, but be called with a paramater:

if (cr_series.length > 1) preload(''cr'');

I don''t know how to take the passed paramated and use it on the ''left''
side of an expression to dynamically create a variable name, etc....
can anyone point me in the right direction?

thanks,
cjl

解决方案

cjl wrote:

Hey all:

I have the following (ugly) code:

function preload()
{
if (cr_series.length >1) preloadCR();
if (ct_series.length >1) preloadCT();
if (mr_series.length >1) preloadMR();
if (us_series.length >1) preloadUS();
if (xr_series.length >1) preloadXR();
}

function preloadCR()
{
cr_images = new Array();
for (var loop = 0; loop <= (cr_series.length-2); loop++)
{
cr_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
cr_images[0][i] = new Image();
cr_images[0][i].src = "images/cr" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}

function preloadCT()
{
ct_images = new Array();
for (var loop = 0; loop <= (ct_series.length-2); loop++)
{
ct_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
ct_images[0][i] = new Image();
ct_images[0][i].src = "images/ct" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}
As you can see, the two preload functions (and the others I didn''t
paste here) are all but identical, except for the names of the arrays
created and used.

I am a beginner, and having trouble writing a generic function that
could do the same thing, but be called with a paramater:

if (cr_series.length > 1) preload(''cr'');
That''s the right idea...

When posting code, use indents of 2 or 4 spaces, not tabs, and manually
wrap lines at about 70 characters to prevent auto-wrapping. It makes
life for those who would reply much easier.

I don''t know how to take the passed paramated and use it on the ''left''
side of an expression to dynamically create a variable name, etc....
can anyone point me in the right direction?



Call a function that creates the array, then return a reference to it to
the original function (I presume you want to make these global). Do you
want to load the images into ''cr_series'', ''ct_series'', etc. or a new
variable?

I''ll presume that you created ''cr_series'' etc. as a global variables
elsewhere and that you want to load the images into them. It seems that
the parameter you pass is the directory name where the images are
stored, so I''ve passed it as a string.

I''m also a bit bemused about the ''+1'' bit. It''s much easier to make
everything zero-indexed, but if you can''t change that I guess you just
have to work with it (but it makes the code more obtuse and more
difficult to maintain).
function preload() {
if (cr_series.length > 1) cr_series = loadArray(''cr'');
if (ct_series.length > 1) ct_series = loadArray(''ct'');
// and so on...
}

function loadArray( parm ) {
var A = [];
for (var loop=0, len=(cr_series.length-2); loop<=len; loop++) {
A[loop] = [];

for (var i=0; i<4; i++ ) {
A[0][i] = new Image();
A[0][i].src = ''images/'' + parm + (loop+1)
+ ''_'' + (i+1) + ''.jpg'';
}
}
return A;
}

A is local to the ''loadArray()'' function. It creates the array, then
returns a reference to it to the calling function (preload). Because a
reference persists, the array stays alive.

Here is a more concise version of the loadArray function:

function loadArray( d ) {
var A = [];
var j, i = cr_series.length; // no ''- 2'' ... see below

while ( --i ) {
A[i] = [];
j = 5; // not 4 ... see below

while ( --j ) {
A[i][j] = new Image();
A[i][j].src = ''images/'' + d + i + ''_'' + j + ''.jpg'';
}
}
return A;
}

I''ve used a pre-decrement operator ( --i ) so that i will have values
inside the while loop of (whatever-it-is-set-at - 1) down to 1 (when it
hits zero the while exits *before* doing the loop content). Same for j.
Saves all the +/-1 stuff.

You may want to pass the value for j as a parameter too, hard-coding ''5''
inside the loadArray function reduces it''s re-usability.

Untested of course, but it should work (OK, maybe a little debugging
required...).
--
Rob


Rob:

Thanks for the quick reply. I will follow your advice for posting code
in the future.

You wrote:

I''ll presume that you created ''cr_series'' etc. as a global variables
elsewhere and that you want to load the images into them. It seems that
the parameter you pass is the directory name where the images are
stored, so I''ve passed it as a string.
You presumed correctly. There is a subdirectory named ''images''. In it
the user will place the relevant images, adhering to a naming
convention. If there are for example two series of CR images and one
series of CT images, the directory might contain "cr1_1.jpg, cr1_2.jpg,
cr1_3.jpg,cr1_4.jpg, cr2_1.jpg, cr2_2.jpg, ct1_1.jpg, ct1_2.jpg,
ct1_3.jpg, ct1_4.jpg, ct1_5.jpg" etc, you get the picture. Then the
user edits the arrays in a seperate js file, such that in this example
there would be:

cr_series = [4,2,0];
ct_series = [5,0];
I''m also a bit bemused about the ''+1'' bit. It''s much easier to make
everything zero-indexed, but if you can''t change that I guess you just
have to work with it (but it makes the code more obtuse and more
difficult to maintain).
Some of the users will not understand zero-index, and the naming
convention for the images will suffer.

function preload() {
if (cr_series.length > 1) cr_series = loadArray(''cr'');
if (ct_series.length > 1) ct_series = loadArray(''ct'');
// and so on...
}

function loadArray( parm ) {
var A = [];
for (var loop=0, len=(cr_series.length-2); loop<=len; loop++) {
A[loop] = [];

for (var i=0; i<4; i++ ) {
A[0][i] = new Image();
A[0][i].src = ''images/'' + parm + (loop+1)
+ ''_'' + (i+1) + ''.jpg'';
}
}
return A;
}



I think there is a problem with this code, which is exactly the
question I was trying to ask in my original post.

Your code has cr_series.length "hard-coded" into the loadArray
function. This array actually needs to change depending on the
paramater passed to the function. If I want to preload CR images, then
cr_series.length is fine, but your example would not work for CT
images.

Let me try to explain the generic function I would like to write. If
the passed parameter is "cr", then it should create an
(multidimensional)array cr_images like my original code does. If I pass
"ct" then it should created a (multidimensional) array ct_images.
These arrays will then be accessed later in the code.

something like:
preload(foo)
{
''foo''_array = code goes here;
}

Is this possible?

thanks again,
cjl


Hey guys;

Another thought just occured to me...
is eval() what I am looking for to dynamically create variables, etc.?

-CJL


这篇关于帮助重构功能......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆