Skip to main content

Bug Tracker

Side navigation

#12041 closed bug (duplicate)

Opened July 08, 2012 08:21AM UTC

Closed July 12, 2012 01:42AM UTC

Last modified July 12, 2012 01:42AM UTC

trigger method is trying to access object's properties as functions

Reported by: ido@easyhi.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:
Description

When using custom events on a JavaScript object (such as window, document or a custom object), if using an event name that equals to a property name on that object, the trigger method will try to run it as a function, throwing an error if it's not a function type.

i.e.

$(document).trigger('width');

Will throw: TypeError: Property 'width' of object #<HTMLDocument> is not a function (on WebKit)

The same will happen if I'm using a namespace in my custom events:

$(document).trigger('width.change');

If the property is a function the trigger method will call it.

$(document).trigger('open');

Will cause the page on Chrome to go blank, for example, same as executing:

document.open();

In my experience it came as a problem when using custom events attached to custom object, as such:

var obj = {a: 1, b: 2, c: 3};
$(obj).on('a.change', function(e) {...});
$(obj).trigger('a.change');

This will cause an error since the trigger method is going to try and run 'a' as a function.

Tested on all major browsers (Chrome, FF, IE8+, Safari).

Attachments (0)
Change History (3)

Changed July 08, 2012 08:26AM UTC by ido@easyhi.com comment:1

jsFiddle example:

http://jsfiddle.net/UaJWd/

Notice how the rest of the script will not execute after the first JavaScript error.

Changed July 12, 2012 01:42AM UTC by dmethvin comment:2

resolution: → duplicate
status: newclosed

Changed July 12, 2012 01:42AM UTC by dmethvin comment:3

Duplicate of #10907.