1

Possible Duplicate:
JS Object this.method() breaks via jQuery

Hello,

I'm playing around with creating classes in javascript but I don't think I really understand everything.

Here is some (simplified) code I wrote (the .bind() comes from jQuery 1.4.1):

function MyClass(size)
{
    this.myList = new Array(size);
    for (var i = 0; i < size; i++)
    {
        this.myList[i] = "Test " + i;
    }
}

MyClass.prototype.InitCells = function()
{
    $('#grid tbody tr td').bind('click', this.GetValue);
}

MyClass.prototype.GetValue = function()
{
    alert(this.myList[1]);
}

Then I do this in my HTML-file:

var test = new MyClass(10);
test.InitCells();

Then, when I click on one of the <td>'s, I get this error: 'this.myList is null or not an object'

What do I need to do to access myList in the GetValue method?

Community
  • 1
  • 1
Kristof Claes
  • 10,797
  • 3
  • 30
  • 42
  • 3
    Seems like this question gets asked *every day* - the short answer is, you're misunderstanding how functions and the context object (`this`) work in JavaScript. For a longer answer, see: http://stackoverflow.com/questions/1544735/js-object-this-method-breaks-via-jquery (picked at random). – Shog9 Feb 16 '10 at 18:14

0 Answers0