0

In javascript, if i'm not using a class, i can do something like this:

const Module = (function(){
    const myArray = [];
    const myObject = {};
    return {    //this is my interfaces
        myArray,
        myObject,

        myMethod1,
        myMethod2,
        myMethod3,
        myMethod4,

        myMethod99,
    }

    function myMethod1(){}

    function myMethod2(){}

    function myMethod3(){}

    function myMethod4(){}

    function myMethod99(){} 

})

This provides me with some kind of structuring that let me list all the publicly available properties and methods on top so it's easier to navigate between module and see what are the available things in a module, and also it's easier to navigate to the function when using Intellisense from IDE such as Visual Studio Code, just go to the module, click the method you want to go to, then press F12, it will bring you to the function.

Now different case with class in javascript:

class Module{

    constructor(){}

    myMethod1(){}   
    myMethod2(){}   
    myMethod3(){}   
    myMethod4(){}

    myMethod99(){}

}

in this case i can't separate the interface and implementation making it hard to navigate through the code. Is there any possibility that the Javascript can handle this kind of case?

Hans Yulian
  • 1,080
  • 8
  • 26
  • 1
    Any decent IDE will be intelligent enough to jump to method definitions in a class, and probably even list all classes and their methods for you for quick access. This is not a question about Javascript, but about IDEs. – deceze Nov 07 '18 at 12:20
  • I rejected the statement that this is not the question about javascript. It's true that intelligent IDE can do such, but in my environment we can't enforce what to be used by people since they have their own preferences. Some even still use sublime and in some cases when someone need to check the code while not available to access pc and directly from the git, we need to have a way of structuring the code so that we can get as much information as possible even with equivalent of notepad – Hans Yulian Nov 07 '18 at 13:20
  • please read the question more carefully, the point here is asking whether i can put all the important properties and interfaces on top of the code so that we can see what will exists in this file even without IDE – Hans Yulian Nov 07 '18 at 13:22
  • I think you’ve already proven that you can…!? – deceze Nov 07 '18 at 13:23
  • in case where we dont use class, the answer is yes, but we have a new problem here when we are using react and react native, while we can do such thing if we are not using class, now we are facing the react and react native where it uses class everywhere where we unsure whether we can do the same way with class or not, or any approach to make it as similar as possible for it. I am not throwing this question directly for react topic because this going to be general javascript class behaviour – Hans Yulian Nov 07 '18 at 13:25
  • What you are referring to is not the interface and implementation but the prototype and implementation as used by C++, the answer is no and why would you want to? If you use a good editor, like PSPAD or eclipse then you can expand and collapse scopes making it very easy to read. – SPlatten Nov 07 '18 at 13:48
  • Yes, correct, the prototype and implementation like in C++. Well, i think the answer for now is really no. About why i want to, because we apply this kind of practice so that even even when i am away from my pc and i need to guide my subordinates, i can easily find out what we did before in certain modules even when opening it using git in my phone. doing that i dont need to make them wait for me to get to my pc and open the source code in a powerful IDE just to find out what are the available methods and properties in a module. – Hans Yulian Nov 08 '18 at 04:02

1 Answers1

0

There are no private members in JavaScript, yet. There exists a proposal which includes this feature, and while it is at Stage 3, it has not yet landed.

There are also no interfaces in JavaScript, (since there's barely even a type-system) and it's unlikely that one will be added in the future.

Consider using TypeScript, it has both of those features.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308