How to create public and private members?

I am a little confused how I can create public and private members.

My code template is still similar:

(function()){ var _blah = 1; someFunction = function() { alert(_blah); }; someOtherFunction = function() { someFunction(); } }(); 
+4
source share
6 answers

You can use the Yahoo module template :

 myModule = function () { //"private" variables: var myPrivateVar = "I can be accessed only from within myModule." //"private" method: var myPrivateMethod = function () { console.log("I can be accessed only from within myModule"); } return { myPublicProperty: "I'm accessible as myModule.myPublicProperty." myPublicMethod: function () { console.log("I'm accessible as myModule.myPublicMethod."); //Within myProject, I can access "private" vars and methods: console.log(myPrivateVar); console.log(myPrivateMethod()); } }; }(); 

You define your private members, where myPrivateVar and myPrivateMethod , and your public members, where myPublicProperty and myPublicMethod defined.

You can simply access public methods and properties as follows:

 myModule.myPublicMethod(); // Works myModule.myPublicProperty; // Works myModule.myPrivateMethod(); // Doesn't work - private myModule.myPrivateVar; // Doesn't work - private 
+13
source

Not. You can rely on the convention by adding _ to your private attributes, and then not touching their code, which should not use it.

Or you can use the function pane to create variables that cannot be accessed from outside.

+1
source

In javascript, each member of an object is public. The most popular way to declare a private field is to use an underscore in the name, just to let others know that it is a private field:

 a = {} a._privateStuff = "foo" 

Another way to hide a variable is to use areas in javascript:

 function MyFoo { var privateVar = 123; this.getPrivateVar = function() { return privateVar; } } var foo = new MyFoo(); foo.privateVar // Not available! foo.getPrivateVar() // Returns the value 

Here is an article that explains this technique in detail:

http://javascript.crockford.com/private.html

+1
source

Three things:

  • IIFEs:
    It seems you need to refresh your knowledge regarding this template. See this post before reusing IIFE.

  • Global public versus safe public:
    Skipping the var keyword with someFunction and someOtherFunction results in the registration of these functional objects in the global scope, i.e. These functions can be called and reassigned from anywhere in the code. This can lead to serious errors as the code gets larger. Instead, use the module template as Daniel . Although you can use other methods, such as Constructors, Object.create (), etc., but it is quite simple to implement.

     /* create a module which returns an object containing methods to expose. */ var someModule = (function() { var _blah = 1; return { someFunction: function() {}, someOtherFunction: function() {} }; })(); /* access someFunction through someModule */ someModule.someFunction(); // ... // .... /* after 500+ lines in this IIFE */ /* what if this happens */ someFunction = function() { console.log("Global variables rock."); }; /* Fortunately, this does not interfere with someModule.someFunction */ 
  • Convention and scope together:
    We cannot expect every developer to adhere to the principle of underline or capitalization agreement. What to do if one of them forgets to implement this technique. Instead of just relying on convention ( _private , GLOBAL ) and scope (function scope), we can combine both of them. This helps maintain a consistent coding style and ensures proper security for members. So, if the next time someone forgets the capital letters of their global, the console (in strict mode) can prevent the world from ending.

+1
source

Javascript has no real private members. You must abuse the area if you really need privacy.

0
source

Source: https://habr.com/ru/post/1309326/


All Articles