1

I am trying to understand a JavaScript code.

Banana.reloadUser is being called inside a function without any arguments:

 function(data) {
                if (!data.result.success) {
                    alert(data.result.message)
                } else {

                    /*do something*/

                    Banana.testData = data;

                    Banana.reloadUser();
                } 
}

Banana.reloadUser defined like this:

Banana.extend({
        reloadUser: function(cb, data) {

            var that = this,
                done = function(d) {
                    $.extend(that.user, d);
                    if ($.isFunction(cb)) {
                        cb(d)
                    }
                    that.trigger("user.reloaded", [d])
                };
            if (data) {
                done.apply(banana, [data])
            } else {
                /*do something*/

            }
        }

})

'reloaduser' is being called to save the userinfo data in the localstorage. So whenever user do something new from its account 'reloaduser' saves the new information into the localstorage.

My question is since Banana.reloadUser is being called without arguments how is it supposed to pick its arguments ?

Note: This is a part of a big JavaScript/jquery code so in case this information is not enough please ignore the question.

The big Javascript code does contain another function

Banana.reloadUser(function() {

                try {
                    Banana.trigger('start', [$]);
                }catch(e) { }

                try {
                    $('[data-deferred]').deferredImage();;
                }catch(e) { }
            });

            started = true;
        };
6
  • 1
    I'm not clear exactly what you mean: does the code work, and you can't explain why it works, or is it broken and you're unsure how to fix it? It's possible that Banana.extend has a mechanism to define the real Banana.reloadUser as a function that calls the function you gave it with the extra arguments. Commented Nov 19, 2014 at 9:53
  • 1
    javascript will set any missing parameters to the value undefined. But question is bit unclear Commented Nov 19, 2014 at 9:53
  • 1
    Yes the code works and I am not able to understand how it works. The 'reloaduser' function is supposed to take the argument 'data' from thee function inside which it is called. Commented Nov 19, 2014 at 9:58
  • @Rup Please see the edited question. I have included another function named 'Banana.reloadUser '. But somehow once the 'Banana.reloadUser' is called, this function is not triggered and instead the code goes inside the other function( .extend) which i already mentioned in the question. Now my question is how these 2 function definition work together when 'Banana.reloadUser' is called Commented Nov 19, 2014 at 15:39
  • OK: I don't know exactly what extend is here, but jQuery's extend means add this map of key-value pairs to the object, i.e. defining the function Banana.reloadUser if it followed the same patten. The example you've just added is actually a call to it, not defining it: the function() block there defines a new anonymous function that is passed in as argument cb (= "call back function" I assume) with no data. Commented Nov 19, 2014 at 15:49

2 Answers 2

2

If you call a JavaScript function without arguments then all its parameters receive a value of undefined (not null which is a different value).

So calling

Banana.reloadUser()

is just the same as:

Banana.reloadUser(undefined, undefined)

In your code this is perfectly ok, the condition:

if ($.isFunction(cb)) {

will fail because undefined is not a function, and later on the condition:

if (data) {

will also fail because undefined is treated as equivalent to false when it appears somewhere that a boolean value is expected.

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

Comments

0

It is exactly similar to the below call,

Banana.reloadUser(undefined,undefined);

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.