bamboopanda bamboopanda - 2 months ago 11
jQuery Question

"this" changes value on click

If I have the following code:

function Something() {
this.randomElement = $("#element");

Something.prototype = {

functionOne: function() {

functionTwo: function() { {
this.functionOne(); //this becomes pointed at randomElement

How can I write this in a clean way where I wouldn't have to use
to replace
inside of functionTwo? Since the click event changes the value of this?


Because this is bound to the item that is clicked. You need to use bind this.functionOne.bind(this) ); 

or jQuery's proxy. $.proxy(this.functionOne, this) );