Private und public Properties und Methoden mit JavaScript

Einer der schönsten Sätze aus der Objektorientierten Programmierung ist: „Keep it DRY, shy, and tell the other guy“. Das shy bezieht sich in diesem Fall auf die Datenkapselung, welche verantwortlich für den kontrollierten Zugriff auf Methoden bzw. Attribute von Klassen ist. Die wichtigsten Zugriffsarten sind dabei „private“ und „public“, welche sich auch mit JavaScript modellieren lassen:

Beispiel:

var myModule = function(){
  var privateProperty = "Top Secret!";
  var privateMethod = function(){
    alert("Private: "+privateText);
  }
 
  return {
    publicProperty: "Hello World!",
    publicMethod: function(){
      console.log("My private property: "+privateProperty);
      console.log("My public property: "+this.publicProperty);
    }
  };
}();

Aufruf:

myModule.publicMethod();

Vielen Dank an das Module Pattern von Douglas Crockford.

Weitere Infos:
A JavaScript Module Pattern, Eric Miraglia
Introduction to the JavaScript Module Design Pattern, Colin O’Dell
JavaScript Module Pattern: In-Depth, Ben Cherry
Learning JavaScript Design Patterns, Addy Osmani

Setting System properties from command-line

Code Sample:

public static void main(String[] args)
{
  String property = System.getProperty("my.text");
  System.out.println(property);
}

Execution:

java -Dmy.text="Hello World." -jar /home/user/application.jar

Note: With java -D you can set every property you like except the reserved ones, listed in [post id=2219]Java System Properties[/post].

You can set multiple properties with the following command:

java -Dmy.text="Hello World." -Dmy.ide="NetBeans IDE" -Dmy.browser="Mozilla Firefox" -jar /home/user/application.jar