1

We know when we are using Object Literals or when we employ Object.Create() to inherit a new Object of a parent one, the child object inherit all the properties and functions of the parent object.

So, how we can hide the private members form the inherited class?

So if we can't, it means using function as class and the "new" keyword to inherit from the parent class, is the only way to reach the ENCAPSULATION in JavaScript!

Amir Jalilifard
  • 2,027
  • 5
  • 26
  • 38
  • 1
    *"how we can hide the private members"* There are no private members (properties I assume) in JavaScript. Could you provide an example of what you are talking about? – Felix Kling Jun 05 '15 at 17:40
  • 1
    Encapsulation can be done through closures in JS. – Shashank Jun 05 '15 at 17:40
  • @Felix Kling of course we have and we should have! This is very obvious! We can use scopes and "var" in a scope to have private members! – Amir Jalilifard Jun 05 '15 at 17:43
  • 1
    @Amir: Of course, but what does this have to do with object literals? – Felix Kling Jun 05 '15 at 17:43
  • 1
    Up through current published standards (ES 5.1), there is no way to have a private property on an object. Though, going forward, [Symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) are being introduced in ECMAScript 6 to support the idea. Accessing the property requires using the same Symbol. – Jonathan Lonowski Jun 05 '15 at 17:44
  • @Felix Kling Object literals have some properties. How we can make a property private in an object literal? – Amir Jalilifard Jun 05 '15 at 17:45
  • 1
    @Amir: You can't. That was the point of my comment. There are no private properties. There are no "private members". What you are referring to is using closures to encapsulate data. I wouldn't call that "private members". – Felix Kling Jun 05 '15 at 17:45
  • possible duplicate of [JavaScript private methods](http://stackoverflow.com/questions/55611/javascript-private-methods) –  Jun 05 '15 at 17:46
  • @Jonathan: You can still iterate over all the properties of the object, whether symbol or not. – Felix Kling Jun 05 '15 at 17:48
  • 1
    @ Jarrod Roberson this is totally a different question with the "JavaScript private methods" discussion. – Amir Jalilifard Jun 05 '15 at 17:49

2 Answers2

1

The easiest way to obtain encapsulation are functions or anonymous functions (closures). This is an example of information hiding and getter/setter logic while classical JS (not ES6/Harmony) does not apparently support it:

var obj = (function () {
  var hidden;

  hidden = 1973;

  return {
    "get": function () { return hidden; },
    "set": function (v) { hidden = v; }
  };
}()); // define and execute right away

You can now use obj as follows:

obj.get()       // returns the hidden value
obj.set(1982)   // sets the hidden value

Demonstration of information hiding:

obj.hidden      // no dice

OO programming in JS does not really support classical inheritance. It supports prototypical inheritance which has its differences.

pid
  • 11,472
  • 6
  • 34
  • 63
  • I already said I know this. I want to know is it possible to achieve Encapsulation in Object literals? – Amir Jalilifard Jun 05 '15 at 17:46
  • Ah ok :) well... I think the comments do already answer very well what is going on with ES5 and ES6... – pid Jun 05 '15 at 17:55
  • I know prototype inheritance and classical inheritance. I am trying to say, In some articles we see that they are trying to bring the class oriented concepts into Object Oriented (let's say prototypal oriented). I can just quote this amazing paragraph from "you don't know JS" book : "I believe the label "prototypal inheritance" itself (and trying to mis-apply all its associated class-orientation terminology, like "class", "constructor", "instance", "polymorphism", etc) has done more harm than good in explaining how JavaScript's mechanism really works." – Amir Jalilifard Jun 05 '15 at 17:58
  • You're right, it did more harm than good. Especially the *bible* by Flanagan was outright misleading up to the 5th edition. We had to wait for Crockford's "Good Parts" to see how well OO pattern really play out without classes. There are many mediocre JS books. Just don't waste time on those :) – pid Jun 05 '15 at 18:02
1

The short answer is No. All properties in Object literals are public (at least as of ES5).

That said, thanks to lexical scoping, Encapsulation does exist in Javascript, and can be achieved through the use of closures.

For example, the object returned by myObjectWithPrivateMembers has access to an encapsulated property, value, which is not publicly visible/accessible (because that object is defined within a closure).

function myObjectWithPrivateMembers() {
    var value = 1;
    var addOne = function() {
        value++; 
    };
    var getValue = function() {
        return value;
    };
    return {
        addOne: addOne,
        getValue: getValue
    };
}

var test = myObjectWithPrivateMembers();
test.value;  // undefined
test.getValue();  // 1
test.addOne();
test.getValue();  // 2
Community
  • 1
  • 1
sfletche
  • 47,248
  • 30
  • 103
  • 119