0

I dont understand javascript syntax well,my question:

How jquery define click function for get parameter inside anonymous function?

The Case:

$("a").click(function(event) { 
    alert(event.type); 
});

in C the function should be defined:

void click(fn,event){
}

in javascript its looks to me that she defined as- (but where defined event?):

click (fn){

}

please explain to me the jquery syntax of click function code source here.

Thanks, Yosef

6
  • I'm not really sure what you want to know. Do you want to know how event is passed to the anonymous function? Btw. the C equivalent would be to pass a function pointer to click which accepts a parameter, not both, function pointer and parameter. In JavaScript you can just write this inline. It is the same as function handler(event) {/*...*/} $("a").click(handler);. There is nothing special about it. Commented Apr 25, 2011 at 23:09
  • Thank you, I think the best expain that I will understand its takes jquery implementation of click and show how its working inline, please can you do it?code.jquery.com/jquery-latest.js Commented Apr 25, 2011 at 23:16
  • Please don't tag your question title like that. Real tags are enough! Commented Apr 25, 2011 at 23:18
  • @Yosef: click is just a shorthand for bind which itself uses jQuery.event.add to attach the event handler. You have to look at this implementation. But the code is quite complex and optimized. In the end it is the same as what is shown in the answers you already have. And still, I think you have to phrase your question better. Commented Apr 25, 2011 at 23:20
  • ok thanks, but I asking because I don't understand that syntax please explain to Commented Apr 25, 2011 at 23:25

3 Answers 3

2

If you just want to find out where the event object is passed to your handler, that would be line 2568 of the jQuery-1.5.2 redistributable source code (or line 438 of the actual, un-contatenated source file):

var ret = handleObj.handler.apply( this, args );

In the above line of code, handler is your anonymous function and args is an array whose first element is the event object. jQuery uses the apply method of the JavaScript Function object to invoke the handler and pass in the arguments

The jQuery source code is quite complex when it comes to full sequence of adding and handling events so, unless you want a line-by-line explanation of hundreds of lines of code, I suggest you rephrase your question to a smaller scope (e.g. You could create a toy demonstration of the scenario you want to understand).

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

Comments

1

Perhaps this will help?

dosomething(function(message) {
    alert(message);    
});

function dosomething(fn) {
    fn("Hello!");
}

2 Comments

thanks for example, but how jquery doing that in source code?code.jquery.com/jquery-latest.js
1

The first part of the jQuery is the selector $("a") which selects and returns object(s) selected from the DOM. In this case, it will return a list of all anchor tag objects on the page.

Then, you are chaining the .click() method to that, jQuery attaches an event listener to all of the anchor tags. When the event listener is attached, it is more or less the equivalent of doing

<a href='..' onclick='someFunction(event)'>some link</a>

...which passes the event object to the function.

For example, compare to this:

<a onclick='blah(event)'>click</a>
<script type='text/javascript'>
function blah(e) {
  alert (e.type);
}
</script>

If I click on it, I will see "click" in the alert. In principle, jQuery is doing the same thing.

5 Comments

Thanks, I understand what you write but not how jquery defined to pass event/e object in click jquery function.if you can show this in jquery source code please
jquery doesn't define the event. "event" is a built-in javascript object reference, kind of like "this". All jQuery does is use it as an argument in the event listener it attaches to the object, just like you would do with normal javascript (see the "compare to this" example I included in the post). Go ahead and c/p that example script to a test page and see for yourself. You aren't defining "event". It's a built-in javascript object reference
ok, but the "click(fn) function" get anonymous function with event and not only event vs blah(e) -(your example function)
Okay but you need to put it all together. When you first use the jQuery selector, it returns a list of objects. Then you chain that with the click method, which attaches an event listener to each object. Yes, it is an anonymous function, but the function is being called with "event" passed to it, which is a built-in, javascript native object reference to the thing you just clicked
See javascriptkit.com/jsref/event.shtml for info about the event object

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.