Bug Tracker

Modify

Ticket #7930 (closed bug: fixed)

Opened 3 years ago

Last modified 2 years ago

Methods on custom objects are being called when an event with the same name is triggered on it

Reported by: timmolendijk Owned by: dmethvin
Priority: low Milestone: 1.next
Component: event Version: 1.4.4
Keywords: Cc:
Blocking: Blocked by:

Description

I don't understand why this happens:

var customObject = { hello: function() { alert("Hello!"); } };
$(customObject).trigger('hello');  // An alert will pop up!
$(customObject).triggerHandler('hello');  // No alert will pop up.

Why is customObject.hello being called when an hello event is triggered on customObject? I wasn't expecting it and I cannot find any hint towards it in the docs. My guess is that it's an unintended side-effect of the new (explicit) support for using jQuery.fn.trigger on custom objects. Am I right?

Change History

comment:1 follow-up: ↓ 2 Changed 3 years ago by dmethvin

  • Owner set to dmethvin
  • Status changed from new to assigned

It's meant to handle the situation where an event named X is triggered on a DOM element and there's a corresponding X method on the element. Events like change, focus, and blur for example.

So yes, in those cases it will call the correspondingly named function on the plain object if it exists. Actually it will try to call the property regardless of whether it's a function or not, because it's impossible to tell with IE.

Sounds like we should put a check into jQuery.event.trigger to see if the element is a DOM element, before someone decides this is a feature.

comment:2 in reply to: ↑ 1 Changed 3 years ago by jitter

  • Priority changed from undecided to low
  • Component changed from unfiled to event

I already assessed this one yesterday but didn't come around to add my findings. Sorry Dave. I basically figured out the same thing as you (code branch in trigger() is only meant for DOM element nodes to use the browsers native event method if applicable)

This also is straightforward to fix imo. It should be fine to just change  this line to

!isClick && target && jQuery.acceptData( target ) ) {

This is better readable, uses acceptData instead repeating the very same thing in the conditional. It also has the nice side effect of fixing a potential other bug: that this branch gets run even when target is e.g. undefined.

So when you tackle this I guess you could do some refactoring and save us a few bytes, as there are at least two other places where a similar pattern (along elem && elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()]) is used which (I guess) can be switch to using acceptData() too. (Maybe even incorporate the check if elem is defined/undefined into the acceptData too?)

Replying to dmethvin:

Sounds like we should put a check into jQuery.event.trigger to see if the element is a DOM element, before someone decides this is a feature.

This "faulty" undocumented behavior is in jQuery since 1.1 (!!!!) as you can see with this  live test case. So by now there surely are some people relying on this hidden "gem" but this should get fixed anyway.

Version 1, edited 3 years ago by jitter (previous) (next) (diff)

comment:3 Changed 3 years ago by scott.gonzalez

Whether or not this is changed should be defined by #7818.

comment:4 Changed 3 years ago by rwaldron

#9146 is a duplicate of this ticket.

comment:6 Changed 3 years ago by rwaldron

#7930 is a duplicate of this ticket.

comment:7 Changed 3 years ago by dmethvin

  • Status changed from assigned to closed
  • Resolution set to fixed

This behavior has been institutionalized and will be documented; see #7818 for the draft documentation about what is valid to call on a jQuery-wrapped plain object. Per the fix for #6170 this behavior is not allowed for window objects to avoid calling globals.

comment:8 Changed 2 years ago by rwaldron

#10747 is a duplicate of this ticket.

comment:9 Changed 2 years ago by dmethvin

#10837 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.