celian-garcia Posted May 22, 2014 Share Posted May 22, 2014 Hello !I use an Object Oriented Javascript code and I'm relatively beginner in that. Here is what my classes look like :/******************************************************************************* * MyProject.MyClass.js * * MyClass class : * Description of the class * * @author: Célian GARCIA ******************************************************************************/(function () { /* Constructor */ MyProject.MyClass = function (p1, p2, ...) { this._privateAttr1 = ... ; this._privateAttr2 = ... ; this._privateAttr3 = ... ; this._privateAttr4 = ... ; this.publicAttr1 = ... ; this.publicAttr2 = ... ; this.publicAttr3 = ... ; this.publicAttr4 = ... ; this.init(); }; /* List of Methods */ MyProject.MyClass.prototype = { init: init, _privateFunc1: _privateFunc1, _privateFunc2: _privateFunc2, publicFunc1: publicFunc1, ... }; function init () { /* Call of building functions */ }; function _privateFunc1 (...) { ... }; function _privateFunc2 (...) { ... }; function _publicFunc1 (...) { };})();Questions are : - Do you think it is a good practice ? - If not, what enhancements do you precognize ? Quote Link to comment Share on other sites More sharing options...
kuuuurija Posted May 22, 2014 Share Posted May 22, 2014 I personally do like this, I make everything public. If I need something to be private, I just type comment them as private. Also I would suggest to look into TypeScript.var MyClass = (function(){ "use strict"; /** * Constructor */ function MyClass() { // public properties this.userName = "meow"; this.age = 25; } MyClass.prototype.someFunction = function () { // do some stuff }; return MyClass;}());// create instancevar myClass = new MyClass();myClass.someFunction(); Quote Link to comment Share on other sites More sharing options...
celian-garcia Posted May 22, 2014 Author Share Posted May 22, 2014 Okay okay it is another way to make. Whereas I'm not convinced I like the underscore to make it private because we directly know if this is private or public. You're right TypeScript seems to be the cleanest way, but I'm a little out of time to learn a new syntax now. :/In a future project I'll certainly think of it ! Quote Link to comment Share on other sites More sharing options...
codevinsky Posted May 22, 2014 Share Posted May 22, 2014 I'd suggest taking a look at this: http://addyosmani.com/resources/essentialjsdesignpatterns/book/ There are a lot of ways to construct classes, and this book details most of them in depth. I'm partial to the Revealing Module Pattern. kuuuurija and Mike 2 Quote Link to comment Share on other sites More sharing options...
celian-garcia Posted May 22, 2014 Author Share Posted May 22, 2014 Thank you, I think there is all I need in this book Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.