嵌套功能 [英] nesting functions

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

问题描述

我正在尝试对某些输入数据进行多次检查。我通过许多函数运行数据来做这个

。即我有一个

onclick调用一个函数,然后调用测试函数。


我的问题是让测试停止,如果其中一个测试失败,并且等待输入被修改。

我相信这是因为测试功能何时完成它

返回在调用函数后立即执行脚本,

即它只是继续进行下一次测试,从而完成

脚本。


如何克服这个问题?


非常感谢

Phil

I am trying to carry out multiple checks on some input data. I am doing this
by the running the data through a number of functions. i.e. I have an
onclick that calls a function, which in turn calls the test functions.

My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?

Many thanks
Phil

推荐答案

让每个函数返回一个布尔值,表明它的成功。对不起js isn'n

我的强项我只是在等待我的一个帖子回答的时候:p,

所以语法可能会偏离。但一般的想法是:


函数checkTheInput()

{

if(firstCheck())

{

if(secondCheck())

{

if(thirdCheck())

{

提醒(''所有3个人都很好'');

}

其他

{

alert(''thirdCheck()失败'');

}

}

其他

{

alert(''secondCheck()失败'');

}

{

否则

{

alert(''firstCheck()失败'');

}

}


函数firstCheck()

{

//如果数据正常则返回true,否则返回false

}


函数secondCheck()

{

//同上

}


等......


" Philip WATTS" < PR ***** @ syringa.freeserve.co.uk>在消息中写道

news:bn ********** @ news8.svr.pol.co.uk ...
Have each function return a boolean indicating it''s success. Sorry js isn''t
my forte I''m just lurnking while I wait for an answer to one of my posts :p,
so syntax could be way off. But general idea is this:

function checkTheInput()
{
if (firstCheck())
{
if (secondCheck())
{
if (thirdCheck())
{
alert(''all 3 cheks were ok'');
}
else
{
alert(''thirdCheck() failed'');
}
}
else
{
alert(''secondCheck() failed'');
}
{
else
{
alert(''firstCheck() failed'');
}
}

function firstCheck()
{
//if data is ok return true, otherwise return false
}

function secondCheck()
{
//ditto
}

etc...

"Philip WATTS" <PR*****@syringa.freeserve.co.uk> wrote in message
news:bn**********@news8.svr.pol.co.uk...
我正在试图携带对某些输入数据进行多次检查。我通过许多函数运行数据来做
。即我有一个调用函数的onclick,它反过来调用测试函数。

我的问题是如果其中一个测试失败并等待测试停止等待对于要修改的输入。
我认为这是因为当测试函数完成
时它会立即将脚本返回到调用函数后的位置,即它只是简单地进行接下来的测试,然后完成
脚本。

如何克服这个问题?

非常感谢
Phil
I am trying to carry out multiple checks on some input data. I am doing this by the running the data through a number of functions. i.e. I have an
onclick that calls a function, which in turn calls the test functions.

My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the script.

How do I overcome this problem?

Many thanks
Phil



>我试图对一些输入数据进行多次检查。我正在通过许多函数运行数据来执行此
> I am trying to carry out multiple checks on some input data. I am doing this
。即我有一个调用函数的onclick,它反过来调用测试函数。

我的问题是如果其中一个测试失败并等待测试停止等待对于要修改的输入。
我相信这是因为测试功能何时完成它
在调用函数后立即将脚本返回到该点,即它只是简单地进行接下来的测试,然后完成
脚本。

如何解决这个问题?
by the running the data through a number of functions. i.e. I have an
onclick that calls a function, which in turn calls the test functions.

My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?




每个如果输入正常,则函数返回true。然后使用if语句。

if(test1()){

if(test2()){

if(test3()){

...

http: //www.crockford.com/#javascript



Have each function return true if the input is ok. Then use if statements.
if (test1()) {
if (test2()) {
if (test3()) {
...

http://www.crockford.com/#javascript


Philip WATTS写道:
Philip WATTS wrote:
我的如果其中一个测试失败并等待输入被修改,问题是让测试停止。
我认为这是因为测试功能何时完成它
返回脚本在调用函数后立即执行,即它只是继续进行下一次测试,从而完成
脚本。

如何克服这个问题?
My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?




如果检查失败,你可以简单地返回false'

而不是使用嵌套的if语句。我发现这个更实用,因为你可以添加测试

而不需要进一步的嵌套块语句(并且没有缩进,为了便于起见,应该为了b
$ b):


函数testMe()

{

if(!test1())

返回false;

if(!test2())

返回false;

if(!test3())

返回false ;


返回true; //通过所有测试

}

PointedEars



Instead of using nested if-statements you could simply `return false''
if a check fails. I find this more practical since you can add tests
without further nested block statements (and without indentation which
should have been done then for the sake of legibility):

function testMe()
{
if (!test1())
return false;
if (!test2())
return false;
if (!test3())
return false;

return true; // passed all tests
}
PointedEars


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

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