Hi,
I am having problems with trying to understand how I can inherit private properties in subclasses. For example if I have the following :
function MyClass() {
var myprop1 = 1; // Private to MyClass
var myprop2 = 2; // Private to MyClass
this.mymethod1 = function () {
return(1)
}
this.mymethod2 = function () {
return(2)
}
}
function MySubclass() {
this.mysubmethod1 = function () {
myprop = myprop + 1 // causes error 'myprop' is undefined
return(3)
}
}
MySubclass.prototype = new MyClass();
function test() {
var o = new MySubclass();
window.alert(o.mymethod1()) // displays 1
window.alert(o.mysubmethod1());
}
What I was expecting to happen was that MySubclass inherits from MyClass all the properties and methods. The methods are inherited but the private properties myprop1,myprop2 have not which is causing an error when calling the method mysubmethod1.
It appears that myprop1 and myprop2 are private to the class MyClass. The question I have is how do I make myprop1 and myprop2 available to the subclass Mysubclass without making them public properties?
I hope this makes some sense as I come from an OOP background and am struggling with some of the concepts of Javascript!!
I am having problems with trying to understand how I can inherit private properties in subclasses. For example if I have the following :
function MyClass() {
var myprop1 = 1; // Private to MyClass
var myprop2 = 2; // Private to MyClass
this.mymethod1 = function () {
return(1)
}
this.mymethod2 = function () {
return(2)
}
}
function MySubclass() {
this.mysubmethod1 = function () {
myprop = myprop + 1 // causes error 'myprop' is undefined
return(3)
}
}
MySubclass.prototype = new MyClass();
function test() {
var o = new MySubclass();
window.alert(o.mymethod1()) // displays 1
window.alert(o.mysubmethod1());
}
What I was expecting to happen was that MySubclass inherits from MyClass all the properties and methods. The methods are inherited but the private properties myprop1,myprop2 have not which is causing an error when calling the method mysubmethod1.
It appears that myprop1 and myprop2 are private to the class MyClass. The question I have is how do I make myprop1 and myprop2 available to the subclass Mysubclass without making them public properties?
I hope this makes some sense as I come from an OOP background and am struggling with some of the concepts of Javascript!!