Alexander Solonik
Alexander Solonik

Reputation: 10230

Why is a trigger('click') being returned?

I am new to JS and trying to understand basic concepts. I want to learn object oriented JS, but I am coming to my difficulty. I am trying to understand the usage of the return statement. I was just going through the code in dropdown.js and came across the following line:

return $this.trigger('click')

Now I am used to seeing:

return true 

return false 

And even:

return (1 + 1) > 10 ; 

I know the usage of these return statements. My general overview of the return statement is it is used for returning a value that you would like to use in another place.

I still can not understand why this statement is being used:

return $this.trigger('click'); 

This line can also be found on line 75 on git.

Thank you.

Alex-z.

Upvotes: 4

Views: 154

Answers (4)

roxan
roxan

Reputation: 201

The documentation at https://api.jquery.com/trigger/ says that .trigger returns jQuery object.

It does that so that you can use it in function chaining. Which means you can do: $this.trigger('click').someOtherJqueryFunction();

If you had not return jQuery object you couldn't have done that.

For simple example let's say you've a object call Person that has properties as Home, Dog

Simple example not involving jQuery. Let's say there is a function to set the home and dog but that function returns person object:

function setHomeName(name){
    homeName = name;
    return this; // return person
}

function setDogName(name){
    dogName = name;
    return this; // return person
}

Now you can do person.setHomeName('home name').setDogName('dog name');

Which is equivalent to person.setHomeName('home name'); person.setDogName('dog name').

As person.setHomeName() is returning reference to person object itself we can chain the function together and write all in one lines. This chain as you can see from jQuery example can be pretty long.

Upvotes: 0

NachoDawg
NachoDawg

Reputation: 1544

the this in return $this has no meaning until it enters a context. When it is returned, the function it returned to is told to do something with a this in its own context

Upvotes: 0

waplet
waplet

Reputation: 151

My guess is that. trigger function returns this object. So the whole thing called would be the same as.

var that = $this.trigger('click')
return that;

But as long as you need to trigger some event, f.e. the keydown event was fired, you trigger the needed event and in the same line return that object.

Upvotes: 0

KJ Price
KJ Price

Reputation: 5974

Notice that this is part of the Dropdown.prototype.keydown method, so we are detecting which key was pressed at the line you are referring to

if ((!isActive && e.which != 27) || (isActive && e.which == 27)) {
  if (e.which == 27) $parent.find(toggle).trigger('focus')
  return $this.trigger('click')
}

Char code 27 is the escape key. So if the user hit the escape key, trigger the on click event for this element (dropdown) and return the jQuery object (because of jQuery chaining).

Upvotes: 3

Related Questions