JavaScript encapsulation

So, I studied all the methods focused on object-oriented development, and I wonder about the following examples.

As I understand it (and it makes sense to me) that the following "secret" field is 'private':

var MyObject = function() { var secret = 'sshhh'; this.getSecret() = function() { return secret; } } 

and this is due to the fact that in the security field there is a function available for the internal function, but nothing outside ... so far so good.

But I saw the following (and especially in the book by Douglas Crockford):

 var MyObject = function() { var secret = 'sshhh'; return { getSecret : function() { return secret; } } }(); 

and wondered what the difference is, why is it better? I understand that in this case we don’t even return the same object as the private field, but we don’t see a huge advantage, since you cannot directly access the field in any case.

+4
source share
7 answers

These examples are very different ... The first creates a "MyObject" function , which when called as a constructor using new will have the getSecret function property as a property; the second creates an Object with the attribute "getSecret" function as the property "MyObject".

In this regard, this is similar to the difference between a static method and a public method. In the first case, the method exists only when the constructor is called, and not in the constructor itself. In the second case, there is no constructor.

So let's say you have:

 var MyObject1 = function() { var secret = 'sshhh'; this.getSecret = function() { return secret; } } // ... var MyObject2 = function() { var secret = 'sshhh'; return { getSecret : function() { return secret; } } }(); 

do some tests:

 MyObject1.getSecret(); // TypeError: Object has no method 'getSecret' var m1 = new MyObject1(); m1.getSecret(); // "sshhh" MyObject2.getSecret(); // "sshhh" var m2 = new MyObject2(); // TypeError: object is not a function 

So, MyObject1 is like a class, and MyObject2 is like a static class.

+7
source

In larger objects, having a return object that explicitly displays the return helps you see what is displayed in one place, rather than worrying about missing it.

+1
source

It is not better - or worse. It is used in two different scenarios.

The first example you give will work well for using a function as a "class":

  stuff = new MyObject(); 

The second example is well suited for defining an embedded object with some functions on it / "singleton".

+1
source

If you need more information about this, there is an article linking to it as an expansion module template:

http://www.klauskomenda.com/code/javascript-programming-patterns/#revealing

+1
source

They are functionally the same, at least when it comes to calling new MyObject().getSecret(); . The only difference is that the latter does not need to be called with new , because it is essentially a factory method and creates an object for you.

0
source

All I can say is that the first example is easier to implement. Consider the first variable is a function that returns a simple string with MyObject() .

The second returns an object that returns a "secret" using MyObject.getSecret() .

So, I would rather get the “secret” from the function by calling the previous example (which requires less code to execute the same) if that means “better”.

Crockford might have had a great idea when he used the second example.

0
source

With Javascript, things are not so simple because, as Douglas Crockford described, “objects are collections of name-value pairs,” so we can dynamically define any property for any object at any time. This is one of the ideas for achieving encapsulation in Javascript:

var Person = (function () {var SSN = "; function Person (name, SSN) {this.name = name; / * Prevents any changes to the SSN property * / Object.defineProperty (this," SSN ", {cost: "" writable: false, enumerated: true, custom: false}); this.getSSN = function () {return SSN;}; this.setSSN = function (ssn) {console.log ("Check here for the red ribbon "); SSN = ssn;}; this.setSSN (PLA);} return of the person;}) ();

When an object is created, it executes the IEF (immediate function) and returns the internal function "Person", which contains a special reference to the SSN variable in the external function (ie, closure), this variable can access public methods in the returned object, therefore it mimics behavior, for example, from a Java class.

 var p = new Person("Marcelo","444"); var p2 = new Person("Joe","777"); p Person {name: "Marcelo", SSN: "", getSSN: function, setSSN: function} p2 Person {name: "Joe", SSN: "", getSSN: function, setSSN: function} p.setSSN("111") p2.setSSN("222") p.getSSN() "111" p2.getSSN() "222" p.SSN = "999" p.SSN "" 
0
source

All Articles