Create Class in Javascript example
taken right from here (a hebrew blog, no. 2 in list)
http://www.softwarearchiblog.com/2012/07/jquery-c-java.html
var myNS = myNS || {}; // ns is optional o.c.
myNS.Calculator = function () {
// private members
var value = 0;
var addBy = function (x) {
value += x;
console.log('value = ' + value);
};
var multiplyBy = function (x) {
value *= x;
console.log('value = ' + value);
};
return { // public parts (aka interface)
addBy: addBy,
multiplyBy: multiplyBy
};
};
var calc = new myNS.Calculator();
calc.addBy(4);
calc.multiplyBy(6); // 24
var myNS = myNS || {}; // ns is optional o.c.
myNS.Calculator = function () {
// private members
var value = 0;
var addBy = function (x) {
value += x;
console.log('value = ' + value);
};
var multiplyBy = function (x) {
value *= x;
console.log('value = ' + value);
};
return { // public parts (aka interface)
addBy: addBy,
multiplyBy: multiplyBy
};
};
var calc = new myNS.Calculator();
calc.addBy(4);
calc.multiplyBy(6); // 24
Comments
Post a Comment