javascript - Closure or prototype model for charting library -


for developing javascript charting library, model best prototype or closure?? while developing chart library includes more intraction , animation, encapsulation necessary?

like here,

function main(){  var w = 100;   function chart(){   //main chart rendering  }  chart.width = function(){}  chart.regenerate = function(){}  chart.sort = function(){}  chart.updatelegend = function(){}  chart.startanimation = function(){} return chart; } 

is valid case use javascript closure this?

i know nothing domain, here's example of do:

//define '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 var chart = new charts.chart(); 

if need priviledged members, can rely on closures, not recommend 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 ; }; } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -