1

Is there anyway to create private fields in ecma6 class that will not be inherited in child-class?

EDIT:

Here is example that almost solved problem: http://jsfiddle.net/z911nrt4/2/

(function() {
    'use strict';

    class CSampleClass {
      constructor(value1, value2) {
        if ( this.constructor.name == "CSampleClass" ) {
            this.privateVar = "is here"; //this wont be inherited
        }

        this.v1 = value1;
        this.v2 = value2;
      }
    }

    class CSampleClassChild extends CSampleClass {
        constructor(value1, value2) {
            super(value1, value2);
        }
    }

    var test = new CSampleClass(1,2);
    var test2 = new CSampleClassChild(1,2);

    console.log("privateVar from object(CSampleClass):", test.privateVar);
    console.log("privateVar from object(CSampleClassChild):", test2.privateVar);
})();

Variable is not-inheritable, but in ugly way and of course it is still public-access field.

ElSajko
  • 1,357
  • 3
  • 14
  • 30
  • 1
    http://stackoverflow.com/questions/22156326/private-properties-in-javascript-es6-classes – mariocatch Nov 06 '15 at 15:12
  • 1
    The answer(s) linked by @mariocatch is part of the answer. The second part would be that you would need to block scope both of those classes if they are both defined in the same module. – CodingIntrigue Nov 06 '15 at 15:20
  • @RGraham: creating symbol out of class scope and using it as a key for class variable/field will not prevent from inheriting it, just you wont be able to access it in child-class, but it will be still there. – ElSajko Nov 06 '15 at 15:43
  • Really I don't care about vars being private. Just need to make them not-inheritable. – ElSajko Nov 06 '15 at 15:47
  • @ElSajko: What do you mean by not-inheritable? Do you want them to be *non-overwritable*? – Bergi Nov 06 '15 at 15:50
  • @Bergi: In example above is shown what I mean. I just doesn't need them (these fields) in child-class object instances. – ElSajko Nov 06 '15 at 15:52
  • @ElSajko: you cannot really prevent inheritance in ES6, and whenever one of your child classes calls `super()` it *will* have created these fields (even if they're not accessible to it, as would be the case for `var`s in the super constructor) – Bergi Nov 06 '15 at 15:55
  • It would be nice if you could add real code so that we can infer your requirements from the context – Bergi Nov 06 '15 at 15:57
  • Just looking for workaround, so it will be like classes in C++ where private fields aren't inherited, only these protected. – ElSajko Nov 06 '15 at 15:57
  • 1
    @Bergi: http://jsfiddle.net/z911nrt4/1/ here is some example that works! But is ugly anyway. Maybe there is better way? – ElSajko Nov 06 '15 at 16:10
  • 1
    @ElSajko: OK, so you really meant what you said. But that suggests there is something wrong with your class model, as subclasses by definition should inherit all fields from their parent. Maybe you need a different solution altogether than subclassing? – Bergi Nov 06 '15 at 16:12
  • @ElSajko: A more ES6y way would be to use `if (`[`new.target`](http://stackoverflow.com/q/32450516/1048572)`== CSampleClass)` – Bergi Nov 06 '15 at 16:13
  • 1
    Agree with Bergi. In typical OOP, child class inherits all the properties of parent class, even if they are private. Its just that private properties are not accessible in child class but they are very much there when an instance of child class is created. – Vivek Athalye Nov 06 '15 at 16:16
  • 1
    @Bergi: Subclasses not always inherit all fields.. in other languages private fields aren't inherited, only protected for example in C++. It is handy in situations where base-class needs to store some data because private methods use them, but child-class do the same in other way, so this data will not be needed. – ElSajko Nov 06 '15 at 16:17
  • 1
    @ElSajko: You may want to have a look at [Inheriting private members in C++](http://stackoverflow.com/q/2676443/1048572) - it's just like in JS. If you declare a "private" `var` in the base constructor and have public, privileged methods accessing the variable, then when inheriting you won't have access to the scoped `var` but it exists and can be accessed via the base methods. – Bergi Nov 07 '15 at 12:49
  • Possible duplicate of [How are labels used with statements that are not a loop?](http://stackoverflow.com/questions/8783054/how-are-labels-used-with-statements-that-are-not-a-loop) – Paul Sweatte Dec 23 '16 at 16:18

0 Answers0