Javascript OOP - accessing the inherited property or function from a closure within a subclass
Posted
by
Ali
on Stack Overflow
See other posts from Stack Overflow
or by Ali
Published on 2011-02-26T07:19:50Z
Indexed on
2011/02/26
7:25 UTC
Read the original article
Hit count: 173
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();
© Stack Overflow or respective owner