0

In javascript as I know, we can make class-like object using function but is it possible to set private and public funciton and variable in function? example.

var a = function(){
    this.public = "hello"
    var private = "hello"
    this.public_func = function(){ console.log("private function");};
    var private_func = function(){ console.log("public function");};
    }

so public, public_func are public attribute and private,private_func is private attribute am I right?

pion123
  • 23
  • 4
  • 2
    _JavaScript_ doesn't have the same concept of public and private like this, everything is public, you control visibility through closures. – Paul S. Feb 12 '14 at 01:16
  • You can mimic private properties, but they are actually not properties of the object. – kapa Feb 12 '14 at 01:17
  • You can emulate private members, see Douglas Crockford's [Private Members in JavaScript](http://javascript.crockford.com/private.html). – RobG Feb 12 '14 at 01:30
  • For private members you have to use closures, there is no private modifier. Privileged functions (functions that have access to private members) have to be in the same scope as the members. This means that privileged function that need access to instance specific private members have to be in the constructor function instead of on the prototype. More info about prototype here. http://stackoverflow.com/a/16063711/1641941 – HMR Feb 12 '14 at 05:15

3 Answers3

1

The private and public properties or functions come into effect only when you create an object out of your class a.

So try this in console:

var b = new a();

Then inspect b, and you will see only :

a {public: "hello", public_func: function}
bits
  • 8,110
  • 8
  • 46
  • 55
  • This fixed my issue. This useful question had a score of -1, and this useful answer had 0, I had to change that. – baptx Nov 05 '22 at 14:45
0

The code that works as you described:

var a = function(){
    this.public = "hello";
    var private = "hello";
    this.public_func = function(){ 
        console.log("public function");
        //calling the 'private' function.
        private_func();
    };
    var private_func = function(){ 
        console.log("private function");
    };
}

But this way doesn't inherits.Check prototype inheritance. Take care with reserved words: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

dee-see
  • 23,668
  • 5
  • 58
  • 91
Mardie
  • 1,663
  • 17
  • 27
0

You can mimic the private properties/functions like other class based languages and there are so many ways to do that, for example this is an example of exposing some public members by explicitly returning an object (Fiddle):

var MyClass = (function(){
    var privateProp = "Private property accessed via public function";
    var privateFunc = function(){
        console.log(privateProp);
    };

    // Expose these as public members
    return {
        'publicProp':'Public Prop',
        'publicFunc':function(){
            privateFunc();
        }
    };

})();

// Use
console.log(MyClass.publicProp); // Public Prop
MyClass.publicFunc(); // Private property accessed via public function

It's a big term and you need to read books and probably articles on this topic (OOP JavaScript), you may read Learning JavaScript Design Patterns book online written by Addy Osmani. It's a good one.

The Alpha
  • 143,660
  • 29
  • 287
  • 307