javascript - Proper way to create a reusable jQuery Object -


i'm trying code first non-hacked jquery plug-in , i'm struggling create basic object constructor, public variables, private variables, , functions.

http://jqueryboilerplate.com/ has great guide creating objects extend jquery, don't think right way go generic object not attached dom element.

does have boilerplate template creating basic reusable object? i.e.

var calc = new customcalculator({'starting_value': 42}); calc.add(3); calc.multiplyby(2); alert(calc.total);   // alerts (42 + 3) * 2 = 90 

from code sample seems need basic way of creating js object. trick (but there lots of other ways it):

function customcalculator(options){     var self = this;     self.total = options.starting_value;      self.add = function(term){         self.total += term;     };      self.multiplyby = function(term){         self.total = self.total * term;     }; }  var calc = new customcalculator({'starting_value': 42}); calc.add(3); calc.multiplyby(2); alert(calc.total);   // alerts 90 

you'd still have implement string concatenation, i'll leave you

if want private variables should use closures. wrote article namespaces , modules in javascript you: http://www.kenneth-truyers.net/2013/04/27/javascript-namespaces-and-modules/

and here's 1 private variables: http://www.kenneth-truyers.net/2012/04/22/private-variables-in-javascript/


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -