This is the code
function toDo(day){
// 1. check IF day is saturday OR sunday
if (day==="saturday"||day==="sunday"){
// 2. return the weekendChore function
return weekendChore();
}
else{
// 3. otherwise return the weekdayChore function.
return weekdayChore();
}
}
// These are the functions we will return:
function weekendChore(){
alert("Weekend: walk 9am, feed at 4pm, walk at 10pm");
return 1;
}
function weekdayChore(){
alert("Weekday: feed at 12pm, walk at 1pm");
return 0;
}
I am new to Javascript and trying to learn. Ive searched and didnt find a proper explanation for the role of return 1 and return 0 in the code above mentioned. Can you explain this? Also, can you replay with someother examples? Thanks
This is the code
function toDo(day){
// 1. check IF day is saturday OR sunday
if (day==="saturday"||day==="sunday"){
// 2. return the weekendChore function
return weekendChore();
}
else{
// 3. otherwise return the weekdayChore function.
return weekdayChore();
}
}
// These are the functions we will return:
function weekendChore(){
alert("Weekend: walk 9am, feed at 4pm, walk at 10pm");
return 1;
}
function weekdayChore(){
alert("Weekday: feed at 12pm, walk at 1pm");
return 0;
}
I am new to Javascript and trying to learn. Ive searched and didnt find a proper explanation for the role of return 1 and return 0 in the code above mentioned. Can you explain this? Also, can you replay with someother examples? Thanks
Share Improve this question asked Nov 8, 2013 at 18:26 user2963757user2963757 1071 gold badge1 silver badge12 bronze badges 3-
1
"
// 2. return the weekendChore function
", actually this returns the result returned byweekendChore()
, not the function itself. The same stands for item 3. To answer the question, we need to see the function callingtoDo()
. – Teemu Commented Nov 8, 2013 at 18:32 -
1
To find the answer to your question, you must look at where the
toDo
function is called. – Adam Jenkins Commented Nov 8, 2013 at 18:35 - ToDo just call the day. If is weekend, it execute weekendChore, else weekdayChore. But why those 2 function have a different return (o and 1). This is the question, cause i cannot understand. Thanks – user2963757 Commented Nov 8, 2013 at 18:59
2 Answers
Reset to default 1This is equivalent to
function toDo(day){
// 1. check IF day is saturday OR sunday
if (day==="saturday"||day==="sunday"){
// 2. return the weekendChore function
weekendChore();
return 1;
}
else{
// 3. otherwise return the weekdayChore function.
weekdayChore();
return 0;
}
}
// These are the functions we will return:
function weekendChore(){
alert("Weekend: walk 9am, feed at 4pm, walk at 10pm");
}
function weekdayChore(){
alert("Weekday: feed at 12pm, walk at 1pm");
}
The real use of those 0
and 1
is hard to guess. They're probably used by the code calling toDo
.
Sometimes it's nice to be able to return ASAP (specially if you use a function a lot). I can't explain the certain situation here though, as they're only called once each.
Here's an example of why it's useful if you call it multiple times:
var lastError;
function doStuff(username) {
if (users.create(username)) {
if (users[username].setActive(true)) {
return true;
} else {
return setError('Could not set user to active');
}
} else {
return setError('Could not create user');
}
}
function setError(error) {
lastError = error;
return false;
}
if (registerUser('MyUser')) {
alert('Yay!');
} else {
alert('An error happened: ' + lastError);
}
..pared to this:
function doStuff(username) {
if (users.create(username)) {
if (users[username].setActive(true)) {
return true;
} else {
lastError = 'Could not set user to active';
return false;
}
} else {
lastError = 'Could not create user';
return false;
}
}
Which just means that every single time we need to set an error we have to return false (even though we do it every time). It's really just shorter.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745287886a4620673.html
评论列表(0条)