Reputation: 382
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)})
})
Upvotes: 0
Views: 74
Reputation: 48279
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);
})
Upvotes: 1