Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested class in javascript, inheritance of private methods

i'm quite a newbie in javascript, and i'm spending some time trying to create namespaced objects in js.

Now, that's what i'm trying to do:

MainObject = function() {

    var privateVariable = "i'm private";

    var privateMethod = function() {
        // doSomething
    }

    this.publicMethod = function() {
        // doPublicSomething
    }
}

MainObject.prototype.nested = function() {

    this.publicNestedMethod = function() {

        // that's not working at all
        this.privateMethod(privateVariable);

    }
}

MyObject = new MainObject();

MyObject.publicMethod();
MyObject.publicNestedMethod();

I tried to include the nested class inside the first one, but it's not working also if i try:

this.nested = function() {

    var mainObject = this;

    return {
        publicNestedMethod = function() {
            mainObject.privateMethod();             
        }   
    }
}();

Someone can help me please? i'm gonna loose my mind on this.

Phaedra.

like image 1000
Phaedra Avatar asked Feb 18 '10 07:02

Phaedra


People also ask

Can you have private methods in JavaScript?

JavaScript allows you to define private methods for instance methods, static methods, and getter/setters. The following shows the syntax of defining a private instance method: class MyClass { #privateMethod() { //... } }

Can nested classes be inherited?

A static nested class can inherit: an ordinary class. a static nested class that is declared in an outer class or its ancestors.

Do nested classes have to be private?

As a member of the OuterClass , a nested class can be declared private , public , protected , or package private. (Recall that outer classes can only be declared public or package private.)

Can we access private method from outside class JavaScript?

The private method of a class can only be accessible inside the class. The private methods cannot be called using the object outside of the class. If we try to access the private method outside the class, we'll get SyntaxError . Our private method can be declared by adding the # before the method name.


1 Answers

JavaScript Classes and Inheritance (ES6)

According to ES6, you can use JavaScript classes and inheritance to accomplish what you need.

JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

I'm showing the examples below with variables, but it can be applied also to functions.


Inheritance (1st Approach)

This solution can only be used with getters for your private variables, otherwise your subclass will not get access to them.

class Main {
  constructor() {
    let privateVariable = "private";
    this.publicVariable = "public";

    this.getPrivateVariable = () => {
      return privateVariable;
    }
  }
}

Main.Sub = class Sub extends Main {
  getAllVariables() {
    return this.publicVariable + "-" + this.getPrivateVariable();
  }
}

// Testing...

let main = new Main();
let sub = new Main.Sub();

console.log(main.privateVariable); // undefined
console.log(main.publicVariable); // "public"

console.log(sub.privateVariable); // undefined
console.log(sub.publicVariable); // "public"

console.log(main.getPrivateVariable()); // "private"
console.log(sub.getPrivateVariable()); // "private"
console.log(sub.getAllVariables()) // "public-private"

Nesting (2nd Approach)

Maybe this solution is better for you because it doesn't expose your private variables outside the Main and Nested classes.

class Main {
  constructor() {
    let privateVariable = "private";
    this.publicVariable = "public";
    
    Main.Nested = class Nested extends Main {
      getAllVariables() {
        return this.publicVariable + "-" + privateVariable;
      }
    }
  }
}

// Testing...

let main = new Main();
let nested = new Main.Nested();

console.log(main.privateVariable); // undefined
console.log(main.publicVariable); // "public"

console.log(nested.privateVariable); // undefined
console.log(nested.publicVariable); // "public"

console.log(main.getPrivateVariable); // undefined
console.log(nested.getPrivateVariable); // undefined
console.log(nested.getAllVariables()) // "public-private"
like image 115
Daniel Possamai Avatar answered Sep 25 '22 01:09

Daniel Possamai