Javascript private and public members

This example shows how a private and public method for a javascript object be created.

by FrictionlessPulley

HTML

<div class="temp">abb
</div>

CSS

.temp{width:100px;height:100px;border:1px solid #000000;background-color:#FF0000}
.temp2{width:100px;height:100px;border:1px solid #000000;background-color:#0000FF}

JavaScript

$(function() {
    var colorchanger = (function() {

        /*this is the object which shall be returned by the function*/
        var changerObj = {};

        /*This is the private methos which cannot be accessed by anyone.*/
        var _private = function() {
            $('.temp').removeClass('temp').addClass('temp2');
        }; /*public method attached to the returning object.*/
        changerObj.change = function() {
            _private();
        };
        //return obj here
        return changerObj;
    })(); //run the function which generates the object reference in (colorchanger)
    colorchanger.change();
});