I have recently started with OOP
in JavaScript. And, I am allover confused with these things. I know JavaScript is entirely different from Java. But this is causing problem.
What I am trying to implement:
function myClass() {
//Declare private variable
var privateVar = '';
//To act as constructor
privateFunction('Hello');
//Getter
this.publicFunctionGet = function() {
return privateVar;
}
//Setter
this.publicFunctionSet = function(x) {
privateVar = x;
}
function privateFunction(x) {
this.publicFunctionSet(x);
}
}
//Create object of myClass
var me = new myClass();
alert(me.publicFunctionGet());
This is throwing an error, which says:
Uncaught TypeError: undefined is not a function
For
function privateFunction(x) {
this.publicFunctionSet(x);
}
What should I do? How to implement this part.? have I misunderstood something about JavaScript classes.?