Private & public properties for JavaScript
Here we have a good article about keeping private properties in your JavaScript kinda Java , well we know that JavaScript is not enough good for Object Orientation but it has a lot good parts
is very flexible.
Here we have the normal way for making "Classes"
//this is like the constructor
};
myClass.prototype.myMethod = function () {
//wow a method :D
};
var myClassInstance = new myClass();
Well we know that everybody who use firebug or javascript pretty often could have access to this objects, so a good way for keep this objects not accesible to the window parent object.. is doing this
var myClass = function () {
//this is like the constructor
};
myClass.prototype.myMethod = function () {
//wow a method :D
};
var myClassInstance = new myClass();
});
And this way is to keep your properties private even from your own objects (I took this method from the Douglas Crockford website), the problem here is that you just access from public to private properties, and private don't have access to public.
function privateMethod() {
alert("Hello I'm the private method");
}
return {
variable:1,
method: function () {
alert(this.variable); // shows 1
},
methodCallPrivate: function () {
privateMethod();
}
}
});
var myClassInstance = new myClass();
myClassInstance.method(); // shows 1
myClassInstance.methodCallPrivate(); //shows "Hello I'm the private method"
But I found this way if you want both have access to each other
var _public = {};
var _private = {};
_public.pubVariable = "Hello World from _public.pubVariable";
_private.priVariable = "Hello World from _private.priVariable";
_public.pubMethod= function() {
alert(_private.priVariable);
};
_private.priMethod = function() {
alert(_public.pubVariable);
};
_public.callpriMethod = function() {
_private.priMethod();
};
return _public;
});
var myClassInstance = new myClass();
myClassInstance.pubMethod(); // shows "Hello World from _private.priVariable"
myClassInstance.callpriMethod(); // shows "Hello World from _private.priVariable"
myClassInstance.priMethod(); //Throws an Error
February 14th, 2011 - 13:36
Thank you Air Max, soon I’ll keep posting
March 23rd, 2011 - 15:12
I feel like I’m missing something, because isn’t the above example just a longer way of doing this: http://pastie.org/1706105
Email me if I’m missing something! I want to learn!
March 23rd, 2011 - 16:12
@Zach
Hey Zach I was checking your pastie code , you are setting the properties to “this” what in this case is the class structure, is not wrong at all Is a way to do it but let me show you the difference
var _self = this;
var _public = {};
_public.returnSelf = function() {
return _self;
};
return _public;
};
var obj = new myClass();
console.log(obj); //Object {}
console.log(obj.returnSelf()); //Object {}
console.log(obj == obj.returnSelf()); // false
and yes is a long way to do it , check this snippet
var privateProperty;
var privateMethod = function() {
};
return {
publicProperty: "Hello World",
publicMethod: function() {
}
}
};