score:0

Accepted answer

I know nothing about your domain, but here's an example of what you could do:

//define the 'charts' module
var charts = (function () {

    //Chart constructor
    function Chart() {
    }

    Chart.prototype = {
        constructor: Chart,

        //public Chart API
        width: function () { /*...*/ }
    };

    //public module members
    return {
        Chart: Chart
    };
})();

//using it
var chart = new charts.Chart();

If you need priviledged members, you can always rely on closures, but I do not recommend it because it's less memory-efficient (instances aren't sharing functions).

E.g.

function Chart(width) {
    var _width = width; //private var

    //priviledged function
    this.width = function () { return _width ; };
}

score:0

In the below code ,am not using Prototype as you used. Is there any difference between both the codes.Any way both will return same Object.Is there any difference in handling objects

var charts = (function () {

    //Chart constructor
    function Chart() {
        console.log(this.width())
    }

    Chart.constructor = function() {
    }
    Chart.width = function(){
    }

    };

    //public module members
    return Chart

    })();

//using it
var chart = new charts.Chart();

Related Query