1

How can I get the value of the quantity local variable for the valueChanged method of functionName1 into functionName2 and assign it to a local variable within that?

var functionName1 = function() {
    $newCheckoutNumberPicker.WanSpinner({
        maxValue: 99,
        minValue: 0,
        valueChanged: function(ele, val) {
            var quantity = val              
        }
    });    
}

var functionName2 = function() {
    var test1 = "";
    functionName1();
}
Joshua
  • 648
  • 7
  • 18
Roger
  • 597
  • 9
  • 32
  • 1
    Call `functionName2()` from within the `valueChanged` function, passing `val` as a parameter – Rory McCrossan Nov 09 '16 at 14:51
  • Check javascript closues.. You will get fair Idea of how this works.. `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures` – Atul Sharma Nov 09 '16 at 14:54
  • @Roger can you confirm you need to know the value at time of valueChanged, or just the value itself, which, assuming you're using wan-spinner, you can get with `$newChecjoutNumberPicker.val()` (ie is this an XY Problem?) – freedomn-m Nov 09 '16 at 15:13

3 Answers3

4

You can use callback function:

var functionName1 = function(callback) {
    $newCheckoutNumberPicker.WanSpinner({
        maxValue: 99,
        minValue: 0,
        valueChanged: function(ele, val) {
            callback(val)       
        }
    });    
}

var functionName2 = function() {
    var test1 = "";
    functionName1(function (quantity) {
        // ...
    });
}
Tân
  • 1
  • 15
  • 56
  • 102
0
valueChanged: function(ele, val) {
    myFunc(val);
 }

And then use the val variable when the value is changer in your fonction.

nicovank
  • 3,157
  • 1
  • 21
  • 42
0

Make it global in a higher scope.

$(function(){
    var quantity;

    var functionName1 = function() {
        $newCheckoutNumberPicker.WanSpinner({
            maxValue: 99,
            minValue: 0,
            valueChanged: function(ele, val) {
                quantity = val              
            }
        });
    }

    var functionName2 = function() {
        alert(quantity);
    }
});
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 3
    Global variables are bad, mmmkay. – Rory McCrossan Nov 09 '16 at 14:52
  • @RoryMcCrossan - No, they're not. But anyway, does the edit appease you? – I wrestled a bear once. Nov 09 '16 at 14:54
  • 1
    They really are http://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice – Liam Nov 09 '16 at 14:55
  • @Liam - Anything can have "good" or "bad" properties, but nothing is inherently either. I could just as easily list a bunch of reasons why local variables are "bad".... like not being able to reach them in the outer scope, for one. – I wrestled a bear once. Nov 09 '16 at 14:57
  • 1
    @Iwrestledabearonce. at least you updated unlike some of the other "answers" :) – freedomn-m Nov 09 '16 at 14:58
  • 2
    If `valueChanged` is an async method which i assume it is for the need of the callback then this is rather brittle. If you call `functionName2` before `valueChanged` calls-back you will get the wrong value. Which means you need to know when `valueChanged` has been called, which means you may as well pass a callback in or wrap in a promise. – ste2425 Nov 09 '16 at 15:07
  • @ste2425 on the other hand, if it's a UI feature such as a picker, then this would be fine as it updates the value on change. Better would be to extract the value from WanSpinner as required rather than rely on valueChanged – freedomn-m Nov 09 '16 at 15:09
  • Exactly, the code to execute on that UI event, which in your example calls `functionName2` to get or work with the event data, would still need to when that event has fired in order to know when to get the data. Otherwise you could try to process, `quantity` on page load when the user changes the value 10 min later. EDIT: pinged wrong person sorry – ste2425 Nov 09 '16 at 15:12
  • 1
    "My" example? Not me – freedomn-m Nov 09 '16 at 15:12
  • @ste2425 - True, but I prefer to assume the OP googled first, in which case s/he would have surely stumbled upon one of the dozens of posts about callbacks and promises. I've given the same answer about callbacks and promises several times myself just this week. – I wrestled a bear once. Nov 09 '16 at 15:13