Posted by & filed under 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”

[cc lang="javascript"]
var myClass = function () {
//this is like the constructor
};
myClass.prototype.myMethod = function () {
//wow a method :D
};
var myClassInstance = new myClass();
[/cc]

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

[cc lang="javascript"]
(function(){
var myClass = function () {
//this is like the constructor
};
myClass.prototype.myMethod = function () {
//wow a method :D
};
var myClassInstance = new myClass();
});
[/cc]
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.
[cc lang="javascript"]
var myClass = (function () {
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”
[/cc]
But I found this way if you want both have access to each other
[cc lang="javascript"]

var myClass = (function () {
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
[/cc]

3 Responses to “Private & public properties for JavaScript”

  1. Ramon Lechuga

    @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

    [cc lang="javascript"]
    var myClass = function () {
    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
    [/cc]
    and yes is a long way to do it , check this snippet
    [cc lang="javascript"]
    var myClass = function() {
    var privateProperty;
    var privateMethod = function() {
    };

    return {
    publicProperty: “Hello World”,
    publicMethod: function() {
    }
    }
    };
    [/cc]

Trackbacks/Pingbacks

  1.  Alexander4
  2.  Alexander7