OOP Javascript vs object literal -
i find myself use oop, mean this
function person(name){ return this.name = name; } person.prototype.dateofbirth = function(){ //some stuff here } var john = new person('john'); console.log(john.dateofbirth);
maybe i'm not building framework or whatever sometime group method using object literal instead like
var john = { name:'john', dateofbirth: function(){ // return etc } } john.dateofbirth();
maybe because js client side language oop quite redundant, can u guys share ur oop experience using js? learned basic prototypical inheritance , advance objects of js haven't find use case it.
check out revealing module pattern, in have control on what's exposed , what's not. think gives public/privacy control 1 wish in oop.
var person = (function() { var name = "default"; function dateofbirth() { privatebirthcalculation(); //can invoked inside of scope console.log( 'dateofbirth' ); } function privatebirthcalculation() { console.log( 'my private method' ); } // explicitly return public methods , properties when object instantiated return { dateofbirth : dateofbirth, name: name }; })();
Comments
Post a Comment