Private class in Javascript

Asked

Viewed 410 times

5

I’m looking for a way to make both attributes and methods invisible so they’re not accessible from outside the class. But I also wanted to use the modern approach to do this (class Nomeclass{}). After many attempts I made a different approach to everything I found: although it works exactly as I need it, and, in my view, it becomes much simpler and readable, I don’t know if this is valid and/ or conventional.

Can I do that? Or is there some convention that prohibits this kind of approach?

Watch the code carefully:

'use strict';
//O objetivo dessa class é tornar os dados restritos
function ExemplePrivate(value){
    var _value = value; //Privado
    
    this.set_value = function(val){
        //Poderá fazer uma validação antes
        _value = val;
    }
    this.get_value = function(){
        //Poderá fazer uma validação antes
        return _value;
    }
}

//Class filha que será pública
class ClassPublic extends ExemplePrivate{
    //adicional code
}

 
    var cPublic = new ClassPublic('A class filha foi <b>instanciada</b>');
    document.querySelector('#p1').innerHTML = cPublic.get_value();
    cPublic.set_value('A class filha foi <b>modificada</b>');
    document.querySelector('#p2').innerHTML = cPublic.get_value();
    
    // Note que o resultado é bloqueado
    document.querySelector('#p3').innerHTML += cPublic._value+".";
#p3{color:red}
<!-- Look in JS -->
<p id="p1"></p>
<p id="p2"></p>
<p id="p3"> O resultado para <b>cPublic._value</b> tem que ser <b>undefined</b> porque é invisível: </p>

  • It seems to me that the question is not about private class but about private fields/methods

  • You can explain better in your example, by instantiating the class var cPublic = new ClassPublic what you want to be visible?

  • Note that this example has the result as expected. And, it is working correctly. Answering your question: Only _value must be private, the rest are functions that manipulate it.

2 answers

4

Power, can. Do what you want? No undesirable effects? Only you can answer this. If you do everything correctly you can do it. If you’re not sure about this and you don’t do anything different, don’t do it. It goes for anything in programming. Never do anything if you are not absolutely sure if the result is exactly what you want, neither more nor less.

Don’t mind conventions that prohibit something. This is called "good practice" and look here on the website for what I think about it. You have to know why things, not conventions that tell you to do something.

I find this code ugly because it doesn’t use class on one side and use it on the other. If you want to make the class be private. I have my doubts if that’s what you want. And I think that you have some flaws in this code, but that’s a matter of opinion, because I’m not sure what you want and especially what you need that’s more important than what you want.

Have a good practice that says to use getter and Setter, only in languages of script It makes little or no sense.

If you want to scale class programming, where you need a private class you should use Typescript. Example code:

module Modulo {
    export class ClassePublica {
        private classePrivada : PrivateClass;
        constructor() {
            this.classePrivada = new ClassPrivada();
        }
        public teste() {
            this.classePrivada.teste();
        }
    }
    class ClassPrivada {
        public teste() {
            console.log('it works');
        }
    }
}

I put in the Github for future reference.

That in JS looks like this:

var Modulo;
(function (Modulo) {
    var ClassePublica = /** @class */ (function () {
        function ClassePublica() {
            this.classePrivada = new ClassPrivada();
        }
        ClassePublica.prototype.teste = function () {
            this.classePrivada.teste();
        };
        return ClassePublica;
    }());
    Modulo.ClassePublica = ClassePublica;
    var ClassPrivada = /** @class */ (function () {
        function ClassPrivada() {
        }
        ClassPrivada.prototype.teste = function () {
            console.log('it works');
        };
        return ClassPrivada;
    }());
})(Modulo || (Modulo = {}));
  • 1

    It is because it is ugly that I was left with doubts if I could. But it is simple and easy to understand. Note that I said I’m starting, so it doesn’t make much sense to learn Typescript if I still don’t understand JS very well. (It’s my opinion). I will scan your code to see what I can do better. Ah! my concern with conventions is because I want to adopt good practices to avoid kidney addictions.

  • Of course if you put the commented code would be much more understandable.

  • 1

    I put the TS code more to illustrate what may be different, is not a fundamental part of the answer, and the code transposed to JS is to be even unreadable. I can’t imagine why it doesn’t make sense to learn TS for those who are starting. If you learn TS you don’t need to learn JS. I don’t think you read my answer right. I told you not to adopt good practices because they create addictions.

  • If you think about the fact that I want to learn JS, you’ll understand why it doesn’t make sense to learn TS right now. I don’t want the compiler to understand anything for me (I want to be 100% in control of the production code). This is only a personal option, I have no intention of criticizing anyone who sees TS as a solution.

  • My question is: in your opinion, is following conventions bad? Is that what you said? Send me the link to see more about this issue.

  • So don’t try to do in JS what the language doesn’t predict. And yes, I say don’t follow conventions, this goes very wrong for everything, especially to solve problems that usually appear in computing. I say to learn not to have to follow conventions, which is to have control of everything, the same as you said in the commentary, but your actions and other speeches go in the opposite direction.?

  • Your words: (This is called good practice and look here on the site for what I think about it.) I did not find.

  • https://answall.com/search?q=user%3A101+%22boas+pr%C3%A1ticas%22

  • I understood your point of view. It seems to be very sensible. I will try to study in depth every code I use in my applications. Thanks.

  • @Maniero the ES6 also has a specification for modules, so unless you want to support IE11, I don’t see why not use Can I Use ES6 Modules

Show 5 more comments

1


Module Pattern

From the description of the problem, where you want to separate the private part from the public part, maybe what you’re looking for is the Module Pattern or something similar. I’ve used a variation called Definitive Module Pattern because I think it makes the implementation clearer. In the link the author also explains how the original pattern works.

Using an example:

var module = (function () {

    function ClassePrivada () {
        this.valor = 30,
        this.setValor = function (val) {
          this.valor = val;  
        }
        this.getValor = function () {
          return this.valor;
        }
    }

    // private
    var _private = {
        valor: 10        
    };
    
    // public
    var _public = {
        getValor: function () { 
          return _private.valor
        },        
        setValor: function (val) {
          _private.valor = val;
        },
        getInstancia: function () {
          return new ClassePrivada();
        }
    };

    return _public;

})();


// funcoes privadas
console.log(module.getValor());
module.setValor(20);
console.log(module.getValor());

// classes privadas
var c = module.getInstancia();
console.log(c.getValor());
c.setValor(40);
console.log(c.getValor());

You can also create a module hierarchy that is interesting and allows you to better organize your code by creating a structure similar to namespaces.

If this is not exactly the kind of solution you are looking for, I hope it will help you in creating your solution.

  • Dude, this code was very clear, clean and beautiful. Even if it wasn’t in the new syntax, I think it was really good. I’m going to implement this code of yours.

  • I also like this drawing of Tim. But the module pattern is the same used in the TS implementation in the other answer. I put this answer because you commented that you wanted something in JS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.