So before I always used this method of encapsulating my Javascript:
Classtype = (function (){
//private members
var variable1;
var variable2;
//
//public methods
return {
SetVariable1: function(pvariable){
variable1 = pvariable;
},
GetVariable1: function(){
return variable1;
}
};
})();
$(document).ready(function(){
Classtype.SetVariable1('var');
var t = Classtype.GetVariable1();
alert(t);
});
But today in my Javascript class, the teacher taught us the following:
function Cname(pvariable1, pvariable2)
{
//private members//
var variable1 = pvariable1;
var variable2 = pvariable2;
//Getter
this.Getvariable1=function() {
return variable1;
}
}
$(document).ready(function(){
var cname = new Cname('test1', 'test2');
var r = cname.Getvariable1();
alert(r);
});
Since I'm pretty new to Javascript I was wondering which way of declaring classes/encapsulating my code is the preferred way and why?