omega
omega

Reputation: 43833

How to access a private field from a static method in javascript es6?

I am looking to make a function that is both private and static in javascript. Is there a way to do it?

I know by doing

#myfunc = function() {

}

I can make it private. But when I try to access that from a function marked as static, I get

(node:15092) UnhandledPromiseRejectionWarning: TypeError: Cannot read private member #myfunc from an object whose class did not declare it

How can I fix it?

Upvotes: 4

Views: 2489

Answers (5)

morganney
morganney

Reputation: 13560

But when I try to access that from a function marked as static

When you make a static member it is associated with the underlying class and not an instance. If you want to access a private member from a static method, you need to mark that member as static also.

class Thing {
  static #myfunc = function () {}
  static method () {
    this.#myfunc()
  } 
}

Thing.method()

Here's an example demonstrating how static methods can access static fields and private fields of instances:

class Thing {
    #prop
    constructor(prop) {
      this.#prop = prop
    }
    static #field = 'static field'
    static #myfunc = function (aThing) {
      console.log(`I can only access a ${this.#field} or private fields of an ${aThing.#prop}`)
    }
    static method(aThing) {
      this.#myfunc(aThing)
    }
  }

  Thing.method(new Thing('instance'))
  console.log(Thing.myfunc) // undefined

Upvotes: 0

Guus Ouwerkerk
Guus Ouwerkerk

Reputation: 11

You can simply simulate private static properties by embedding the class definition in a function and use and declare your private static properties using the let statement before you declare your class. These variables will be visible in your class but nowhere else.

     let _myPrivate = 123;  // variable exist the whole lifetime of the page.
     MyClass = class {
       get myPrivate() { return _myPrivate; }     // returns 123
     }

Upvotes: 0

dlopez
dlopez

Reputation: 995

As far as I know, you can't define private static methods with the current ecmascript version. But you can emulate an static method by declaring a function outside the class but within the same file.

While you don't export that function from that file, it will work the same way as a private static method, but the way of calling it.

It's a way to achieve the same behaviour with the current javascript specification.

// MyFile.js

function myPrivateMethod() {} // You won't have access to this function outside this file

export class MyClass {
   myMethod() {
     myPrivateMethod();
   }
}

If you want to access static properties, you just need to call MyClass.myStaticProperty. In that case I suggest you to define the function after the class definition, to avoid hoisting issues.

Upvotes: 4

D. Pardal
D. Pardal

Reputation: 6587

I am looking to make a function that is both private and static in JavaScript.

That's not possible, because static properties are properties in the class object (the one you call with new). So if someone has access to the class object, they'll have access to any static properties.

You can, however, use a regular function to replace it. You can even create a object that contains the private "static" functions and call them from that object.

Upvotes: 2

shiponcs
shiponcs

Reputation: 1677

You can't make static property private, or other accessibility. Because, it's by default public and it's always true

It is not possible to specify the accessibility of statics—they are effectively always public.

Upvotes: 2

Related Questions