Jump to content

Design of JS Class


celian-garcia
 Share

Recommended Posts

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 ?

Link to comment
Share on other sites

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();
Link to comment
Share on other sites

Okay okay it is another way to make. Whereas I'm not convinced :P 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 !

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...