1

What's the difference between:

<script type="text/javascript">
        $().ready(function() {
            InitialDictionary = new Array();
            LoadCurrentValues(InitialDictionary);
            $("a[id*=SomeLink]").click(function() {
                if (!CompareDictionaries(InitialDictionary)) 
                {
                    alert('Hello')
                }
            }
        )
        })
</script> 

and

<script type="text/javascript">
    $().ready(function () {
        InitialDictionary = new Array();
        LoadCurrentValues(InitialDictionary);
        $("a[id*=SomeLink]").click(CheckValuesChanged(InitialDictionary));
    })

    function CheckValuesChanged(InitialDictionary) {
        if (!CompareDictionaries(InitialDictionary)) 
        {
            alert('Hello')
        }
    } 
</script>

Without going into details into what I'm trying to achieve here, is there any reason why an anonymous method works fine and the call to a function doesn't? Shouldn't they produce the same results?

3 Answers 3

2

To call a function you have to do this:

$("a[id*=LogoLink]").click(function(){CheckValuesChanged(InitialDictionary)});

Or:

$("a[id*=LogoLink]").click("CheckValuesChanged(InitialDictionary)"); //might work
Sign up to request clarification or add additional context in comments.

3 Comments

No, that won't work. When calling setTimeout, it would work (except for the variable), but it's heavily frowned upon.
@SLaks, hence the might, better to go with option 1
And the first solution still uses an anonymous function. The OP seems to want to do it w/o the anonymous function.
2

They both work, but you cannot bind a function to an event like this

$("a[id*=LogoLink]").click(CheckValuesChanged(InitialDictionary));

because the a function must be bound to the click event. When you pass an argument to the function it returns undefined, which is not a function. You can fix this by changing your second code sample like so:

$().ready(function () {
    InitialDictionary = new Array();
    LoadCurrentValues(InitialDictionary);
    $("a[id*=LogoLink]").click(CheckValuesChanged);

  function CheckValuesChanged() {
      if (!CompareDictionaries(InitialDictionary)) {
                alert('Hello')
      }
  } 
});

4 Comments

InitialDictionary is not in the scope of CheckValuesChanged
not always best to use variables like that, than what is the point in the function if the variable is global?
Declare InitialDictionary outside of any function, right in the begining of your javascript body. Document.Ready function will asign value to it and CheckValuesChanged will be able to access it later.
@Dmitri Neal's point would still stand, because it would rely on an evil global variable.
1

The second example is wrong:

$("a[id*=LogoLink]").click(CheckValuesChanged(InitialDictionary));

It should be:

$("a[id*=LogoLink]").click(CheckValuesChanged);

But because you want to pass InitialDictionary as argument you need to use the first approach which will capture it in the anonymous function.

3 Comments

Not if the OP wants a parameter
Can't I pass a parameter into the function? Because I want to reuse that method in several .click(...) calls.
@Dante, yes you can, using the first approach.

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.