Jump to content

this.array = undeffined


Matej
 Share

Recommended Posts

Hi , im building table spoiler here http://jsfiddle.net/mLjo49rk/2/ (basicly , every text get shortened to length 1 and on click it should display full text)

 

but i have problem with function longIt();

it basicly writes Cannot read property 'length' of undefined while i was working with array this.after in function shortIt,

 

any1 knows how to fix it?

Link to comment
Share on other sites

There might be confusion as to what "this" is referring to because it's inside a function that could be called from any context. Here's the solution: Make an internal variable that is explicitly set to "this" when the object is created and use that variable to refer to the object.

 

You also forgot to define the variable "event", it should be the first parameter of the click event handler.

function matej(element)    var self = this; // Reference to the object    // ...    self.longIt = function() {        for (var i = 0; i < self.children.length; i++) {            self.children[i].onclick = function (e) { // Added event object. Called it "e" so it won't override global variable "event" that IE needs                e = e ? e : window.event; // Compatibility for Internet Explorer                var x=self.after.indexOf(e.target.innerHTML);                alert("!as");            }        }    }    // ...}
Link to comment
Share on other sites

It should be safe to use the "this" keyword when you're adding a function to the prototype, it's only in the class definition where you need to have a clear reference to the object.

 

After correcting that, I still get an error because you haven't defined "event" yet.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...