0

Hi I would like to pass function to another but the without affecting middle function in my code

checkUserAndStart(startFunction) - this function I want to reuse in many places (to check logged user - if is logged start function but not always with parameters)

  function startInTorry(email,displayName){         
      $('#footer-email').html(email);
      $('#InTorryUserName').html(displayName);        
  }

  function checkUserAndStart(startFunction) {
      firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
          //user.email,user.displayName is from here
          startFunction();
        } else {
          window.location = 'login.html'
        }
      })
    }

  $(document).ready(function () {       
    checkUserAndStart(function(){startInTorry(user.email,user.displayName)})
  })
2
  • whats problem you are getting here ? Commented Jun 26, 2019 at 12:20
  • it doesnt display email in $('#footer-email').html(email);ReferenceError: "user is not defined" Commented Jun 26, 2019 at 12:23

1 Answer 1

1

You pass the function without its arguments. Then, when you call it, you call it with arguments.

function startInTorry(email,displayName){         
      $('#footer-email').html(email);
      $('#InTorryUserName').html(displayName);        
}

function checkUserAndStart(startFunction) {
  firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
      startFunction(user.email, user.displayName);
    } else {
      window.location = 'login.html'
    }
  })
}

$(document).ready(function () {
  checkUserAndStart(startInTorry);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Wiktor, The question was not to affect the middle function. I want to reuse it in many places sometimes without parameters. Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.