-2

Let’s say that I have a logOut() function and a user may be logged in via one of several different ways. So in the logOut() function I might check for Facebook auth user context. If that exists then I would execute the code to log the user out of Facebook and then navigate to the Login Home page. Similarly a user may be logged in via Apple and I would handle this scenario in a similar way. Would it be possible to define some type of function variable at the beginning of this logOut() function like:

const navigateHome = props.navigation.navigate('LoginHome');

And then reuse the navigateHome variable as an instruction based on the case in the code like:

if(loggedInViaFacebook){
   navigateHome;
}else if(loggedInViaApple){
   navigateHome;
}

This is a very simple example but I just want to understand if this is conceptually possible so I can leverage it as a technique.

user10307666
  • 151
  • 1
  • 4
  • 14
  • Yes, you can put that function as a local variable inside your `logOut` function, but no, you need to actually write a `function navigateHome() { … }` and then *call* that with `navigateHome();` elsewhere. – Bergi Nov 28 '19 at 23:19
  • Possible duplicate of [Declaring functions in JavaScript](https://stackoverflow.com/questions/1925976/declaring-functions-in-javascript) – Alexei Levenkov Nov 28 '19 at 23:22
  • @AlexeiLevenkov No, not that one – Bergi Nov 28 '19 at 23:24
  • @Bergi to me it looks like OP can't yet define function whatsoever... That post shows plenty of ways to do so... Indeed maybe OP asks for something else - since you understand what actually question is consider to [edit] to clarify... – Alexei Levenkov Nov 28 '19 at 23:28
  • @AlexeiLevenkov The proposed duplicate surely has many examples of functions, but so does any other js topic. The OP didn't ask about the differences between two function syntaxes, he doesn't seem to know how to define a function at all and what the difference to putting code in an assignment is. – Bergi Nov 28 '19 at 23:34

3 Answers3

0
const navigateHome = () => {
   props.navigation.navigate('LoginHome');
}

Or

function navigateHome() {
  props.navigation.navigate('LoginHome');
}
Wenbo
  • 1,212
  • 8
  • 17
0

Sure you can. You can have your function declared globally like below:

const navigateHome = () => props.navigation.navigate('LoginHome');
const logoutFB = () => { //doSomething with multiple line};
const pingIP = function (ip){ ...};

And can use anywhere

if(loggedInViaFacebook){
   navigateHome();
 }else if(loggedInViaApple){
   logoutFB();
   pingIP();
 }
Giddy Naya
  • 4,237
  • 2
  • 17
  • 30
  • OP does *not* want a globally declared function, he explicitly asked to "*define some type of function variable at the beginning of this logOut() function*". – Bergi Nov 28 '19 at 23:35
0

Functions may contain other functions. They can be called within the same scope (assuming you're using const instead of var).

function someParentFunction() {

  const navigateHome = function() {
    props.navigation.navigate('LoginHome');
  }

  if(loggedInViaFacebook){
    navigateHome();
  }
  else if(loggedInViaApple){
    navigateHome();
  }

}