14

A class I'm writing in node.js is as below:

module.exports = exports = function(){ return new ClassA() };

function ClassA(){
    this.myvariable = 0;
}

I have a function that I want to be private. To my understanding if the function is declared outside of the constructor, it will essentially be a static function which wouldn't be able to reference this.myvariable.

Is the correct way of dealing with this to declare the function within the constructor like this:

//within constructor
this.myFunction = function myFunction(){
    console.log(this.myvariable)
}

Or is there a better way of doing it that doesn't leave me with a potentially huge constructor?

EDIT: It looks like I've misunderstood something here because the above code doesn't even work...

7 Answers 7

21

Simplest way to have a private function is to just declare a function outside of the class. The prototype functions can still reference it perfectly fine, and pass their this scope with .call()

function privateFunction() {
  console.log(this.variable)
}

var MyClass = function () {
  this.variable = 1;
}

MyClass.prototype.publicMethod = function() {
  privateFunction.call(this);
}

var x = new MyClass();
x.publicMethod()
13

Update from May 17, 2019

In ESNext, which is the next specification of javascript, the support of private methods and attributes has been added.

To make an attribute or a function private, use the character #.

class Foo {
    #privateAttribute = 'hey';

    showPrivate() {
        console.log(this.#privateAttribute);
    }
}

It has been implemented in node.js v12+.


You can configure babel to transpile it using the following plugin.

It requires babel v7.0.0+.

2
  • Using arrow function you can also define private methods like properties #privateMethod = () => { ... code ... } Oct 27, 2020 at 10:56
  • Private instance variables can be declared like: this.#privateVar = 'asdf';. You can also define private functions by just naming them with a # at the start. #privateMethod() {...}. Doesn't have to be using #a = () => {...} Apr 15, 2021 at 14:19
7

Whatever you will not add to module.exports will be private for that module and can not be accessed from outside of the module. Also inside the controller store the reference of this into a local variable

var self = this;

You can use revealing module pattern.

var myNameSpace = function() {
    var current = null;
    function init() {
        …
    }
    function change() {
        …
    }
    function verify() {
        …
    }
    return{
        init:init,
        change:change
    }
}();
module.exports = exports = myNameSpace;

This way init and change will be public and verify will be private.

You can see Douglas Crockford style of making private members. http://javascript.crockford.com/private.html

Edit
Douglas Crockford's link has been changed now.
new link http://crockford.com/javascript/private.html

1

Yes you can make a private method, but it can't be part of the prototype

function ClassA()
{
    var myvariable;

    var private = function()   // This is private method
    {
        myvariable = 0;
    }

    this.public = function()   // This is public method
    {
        private();
    }
}
1

Javascript Public Functions

ClassA.prototype.myFunction = function (string) {
   //your logic
}

another type of public function

 function ClassA(){
    this.myvariable = 0;

    var MyFunction3 = function () {
       //your logic
    };
    this.MyFunction2 = function () {
        //your logic
    };
 }

javascript Private Function

function ClassA() {

    function MyFunction() {
       //your logic
   }
}

I would like to prefer to visit this link that will describe you every well.

0

In fact ,there are not class in javascript.
Look a json object constructor:

var ClassA=function() {
    this.publicFunction=function (){
        console.log("public function");
    }
    function privateFunction(){
        console.log("private function");
    }
    return this;
}

then you use new to create a ClassA:

var a=new ClassA();

you can use the publicFunction, but you can't use the privateFunction like it's private.

a.publicFunction();
a.privateFunction();//you can't do this

Any help for you?

0
//  NOT A CLASS just a "regular-file.js"

    function A(){}    
    function B(){}
    function C(){}
    module.exports = {
      A,
      B
    }

// Using the functions 

    let function = require('location of regular-file.js')
    function.A()
    function.B()

Function C is NOT exposed and therefore it is private

***************** Private functions in a Class ***************

function outsideClass(){}

class MyClass{
  function insideClass(){
    outsideClass()  //  calling the function outside of the class
  }
}

module.exports = {MyClass}

//  using the functions in the class
let myClass = require('MyClass')
let mc = new myClass()
mc.insideClass()

The function outsideClass() is not exposed and therefore private

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.