Javascript OOP - accessing the inherited property or function from a closure within a subclass
- by Ali
Hi All,
I am using the javascript inheritance helper provided here: http://ejohn.org/blog/simple-javascript-inheritance/
I have the following code, and I have problem accessing the inherited property or function from a closure within a subclass as illustrated below. I am new to OOP javascript code and I appreciate your advice. I suppose within the closure, the context changes to JQuery (this variable) hence the problem. I appreciate your comments.
Thanks,
-A
PS - Using JQuery 1.5
var Users = Class.extend({
init: function(names){this.names = names;}
});
var HomeUsers = Users.extend({
work:function(){
// alert(this.names.length); // PRINTS A
// var names = this.names; // If I make a local alias it works
$.map([1,2,3],function(){
var newName = this.names.length; //error this.names is not defined.
alert(newName);
});
}
});
var users = new HomeUsers(["A"]);
users.work();