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() );
                            },
                        });

                });
..........................
8
  • 5
    You're encountering the effects of basic scoping rules. Commented Aug 14, 2015 at 13:33
  • 2
    This is a matter of scope :) Maybe look it up. stackoverflow.com/questions/500431/… Commented Aug 14, 2015 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. Commented Aug 14, 2015 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." Commented Aug 14, 2015 at 13:37
  • You have to do a global variable, put the variable at the beginning Commented Aug 14, 2015 at 13:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Tried it but didn't work. But I found my answer below. Thanks anyway.
It has to work, just make sure you replace yourObject with the real object you're using (is it self?). You're welcome.
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?
It's hard to see here in the comments... what do you have above get_name: function(){? Inside what function it is?
Should be self. Works with self!
|
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);
                },
            });

});

4 Comments

This is a bad approach, not only because it pollutes the globalspace, but it beats the purpose of that getter function.
code sheets get read from top to bottom: so first the declarations, then the code line and functions using your variables.
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...
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

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.