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