javascriptjqueryprototype-oriented

Combining JS Function.prototype method & jquery event listener


If I create an object constructor such as:

function LayoutObj() {...}

and a method for it:

LayoutObj.prototype.navDates = function () {...}

Can you help me understand the proper syntax for binding this method to an onclick event?

I tried this:

$('.previous').on("click", LayoutObj.navDates());

And received the error:

Uncaught TypeError Object function() LayoutObject{...} has no method 'navDates'

Let me know if you need more info.


Solution

  • Talked to another coder, and he tells me "That's backwards. You'd want to raise an event from an object and have something listen for it. You wouldn't want it to handle an event. You'd raise an event when some criteria is met and the consuming code would handle it."

    So the answer is, you don't! That explains why I couldn't find it, and why no one wanted to answer the question.

    My solution: Create a function that creates the object and executes the method.