1

When I use a variable in javascript, it gives an error

ReferenceError: getalletje is not defined

, depending on where I declare it.

This is where I declare the variable:

get_name: function(){
        var getalletje = 5;
        return getalletje;
    },

Where I'm trying to use the variable:

        self.$('.geldinuit-button').click(function(){

                self.screen_selector.show_popup('geldinuit',{
                    message: _t('Popup titel'),
                    comment: _t('getalletje'),
                    confirm: function(){
                        window.alert(getalletje);
                    },
                });

        });

It gives an error like this.

But: If I put var getalletje = 5;just above self.$('.geldinuit-button').click(function(){, it works.

Something extra I need to do?

Edit for Shomz: This is the full code:

.............                
self.set_smart_status(status.newValue);
            });

            this.$el.click(function(){
                self.pos.connect_to_proxy();
            });
        },
    });

    module.PosWidget = module.PosBaseWidget.extend({
        template: 'PosWidget',
        init: function() { 
            this._super(arguments[0],{});
            this.pos = new module.PosModel(this.session,{pos_widget:this});
            this.pos_widget = this; //So that pos_widget's childs have pos_widget set automatically

.............................
............................

    get_name: function(){
        var getalletje = 5;
        return getalletje;
    },
...........................
...........................
                self.$('.geldinuit-button').click(function(){

                        self.screen_selector.show_popup('geldinuit',{
                            message: _t('Popup titel'),
                            comment: _t('getalletje'),
                            confirm: function(){
                                window.alert( this.get_name() );
                            },
                        });

                });
..........................
RobbeM
  • 727
  • 7
  • 16
  • 36
  • 5
    You're encountering the effects of basic scoping rules. – Pointy Aug 14 '15 at 13:33
  • 2
    This is a matter of scope :) Maybe look it up. http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – bbill Aug 14 '15 at 13:33
  • 1
    A variable declared inside a function is not available outside that function; that's the whole point of being able to declare *local* non-global variables. – user229044 Aug 14 '15 at 13:34
  • Yes there is something extra you need to do, but you already said what it was: "If I put var getalletje = 5;just above self.$('.geldinuit-button').click(function(){, it works." – Luke Aug 14 '15 at 13:37
  • You have to do a global variable, put the variable at the beginning – bicho Aug 14 '15 at 13:41
  • @bicho so what's the purpose of that getter function if you suggest the global variable? – Shomz Aug 14 '15 at 13:48
  • The purposse of the function is to set a value to global variable, then you could use this variable in any function @Shomz – bicho Aug 14 '15 at 14:04
  • @bicho Umm, no... getter functions **GET** values, they don't **SET** values... – Shomz Aug 14 '15 at 14:06

2 Answers2

1

It's a scoping issue - the variable you declare is available only locally. To get its value, you can do something like:

confirm: function(){
    window.alert( yourObject.get_name() );
},

where yourObject is the object that you defined the get_name method for.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Tried it but didn't work. But I found my answer below. Thanks anyway. – RobbeM Aug 14 '15 at 13:49
  • It has to work, just make sure you replace `yourObject` with the real object you're using (is it `self`?). You're welcome. – Shomz Aug 14 '15 at 13:50
  • Just to make sure: This is my code: module.PosWidget = module.PosBaseWidget.extend({ template: 'PosWidget', init: function() { this._super(arguments[0],{}); this.pos = new module.PosModel(this.session,{pos_widget:this}); this.pos_widget = this; //So that pos_widget's childs have pos_widget set automatically ......................... Is "PosWidget" the object? – RobbeM Aug 14 '15 at 13:54
  • It's hard to see here in the comments... what do you have above `get_name: function(){`? Inside what function it is? – Shomz Aug 14 '15 at 13:56
  • I've put the "complete" code in my OP. I also tried to use this.get_name(). But also didn't work. – RobbeM Aug 14 '15 at 14:01
  • 1
    Should be self. Works with self! – RobbeM Aug 14 '15 at 14:03
  • Still not quite clear, but I think it should be `module.PosWidget`. Oh, so it was `self`, nice. (see my first comment here) :) – Shomz Aug 14 '15 at 14:03
0

you can't access variable outside of the function you declare it in. If you want to access variable in a global way you should declare it outside of get_name. In your case I don't see the point but the following should work:

var getalletje = 0;

get_name: function(){
        getalletje = 5;
        return getalletje;
    },

self.$('.geldinuit-button').click(function(){

                self.screen_selector.show_popup('geldinuit',{
                    message: _t('Popup titel'),
                    comment: _t('getalletje'),
                    confirm: function(){
                        window.alert(getalletje);
                    },
                });

    });

also, shouldn't:

comment: _t('getalletje'),

become

comment: _t(getalletje),

UPDATE

Also, in your case this declaration isn't useful. I don't know if you changed your code when asking your question but in this case doing only:

self.$('.geldinuit-button').click(function(){
                var getalletje = 5;

            self.screen_selector.show_popup('geldinuit',{
                message: _t('Popup titel'),
                comment: _t('getalletje'),
                confirm: function(){
                    window.alert(getalletje);
                },
            });

});
SKYnine
  • 2,708
  • 7
  • 31
  • 41
  • This is a bad approach, not only because it pollutes the globalspace, but it beats the purpose of that getter function. – Shomz Aug 14 '15 at 13:49
  • code sheets get read from top to bottom: so first the declarations, then the code line and functions using your variables. – SKYnine Aug 14 '15 at 13:49
  • I'm sorry I was wrong... Didn't work. I just read the value of the variable defined on the top instead of the value the function should give it... – RobbeM Aug 14 '15 at 13:50
  • yes, cause there is a typo in the way you call you variables. (Or I have a typo). I've updated my answer with some considerations from Shomz – SKYnine Aug 14 '15 at 13:53