1

I'm having trouble finding some information, and it's almost certainly because I don't know the correct terminology for what I'm doing. When I search for info about variables in a callback function, the code not the same as what I am trying to do here.

I have some JavaScript code, this is part of it:

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: functionNameHere,
    timeout: 10
});

The "functionNameHere" part is the name of another function, that is called when the notification created by this script is clicked.

I need to be able to pass a variable along with it, so essentially what i need to do is:

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: functionNameHere('variableContentWouldbeHere'),
    timeout: 10
});

However, when I do it like that it doesn't work properly.

How would I achieve this?

2
  • 3
    notifyClick: functionNameHere.bind(someContext, yourVar) Commented Oct 20, 2016 at 9:30
  • Research 'currying' -- basically using some variant of Maxx's comment Commented Oct 20, 2016 at 9:32

5 Answers 5

2

You can achive this with changing function scope or using closures.

1st:

notifyClick: functionNameHere.bind('variableContentWouldbeHere');

And your context inside "functionNameHere" implementation is your argument.

function functionNameHere() {
   console.log(this === 'variableContentWouldbeHere'); //true
}

2nd:

Using closures

notifyClick: function() { functionNameHere('variableContentWouldbeHere'); }
Sign up to request clarification or add additional context in comments.

2 Comments

Better answer than mine ;-)
I ended up using closures, your second method. Since you were the first to answer with this solution, I'm accepting your answer.
1
var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: function(){
    functionNameHere('variableContentWouldbeHere');    
    },
    timeout: 10
});

2 Comments

Your solution to use closures is what I ended up doing. However, someone else answered with the same solution before you, so i accepted their answer. I did give you an upvote though. Thanks for your help :)
Glad to Thank you too :)
1

I am not totally sure what you are trying to do, but I guess may be you need something like this:

var myVariable = {};
var myNotification = new Notify('Notification Title', {
   body: 'message goes here',
   icon: "/icon.png",
   tag: 'for app use',
   notifyClick: function(e){
      functionNameHere(myVariable);
   },
   timeout: 10
});

2 Comments

Your solution to use closures is what I ended up doing. However, someone else answered with the same solution before you, so i accepted their answer. I did give you an upvote though. Thanks for your help :)
Thanks @SherwinFlight
0

You can't affect how the callback is invoked, the code in the Notify class determines that. If you try to pass it like in your second example, the function "functionNameHere" will be called immediately and the result of it is passed to the Notify constructor as the callback function - which is not what you want.

What you could do is make your functionNameHere function be a wrapper that returns another function that will be used as the callback.

2 Comments

So, basically, unless the Notify class allow variables to be passed, it won't work?
Just use bind and you can bind context and arguments to function
0

You could pass an anonymous function which calls your function like this:

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: () => functionNameHere('variableContentWouldbeHere'),
    timeout: 10
});

Comments

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.