Wednesday, June 5, 2013

Encapsulation exists in JavaScript.

There are not public and private keywords akin to those in C# but you can nest a function within an function making it only accessible from within the function wrapping it. Behold:

function mypublicthing() {
   myprivatething("Hello");
   function myprivatething(greeting) {
      alert(greeting +" World");
   }
}

 
 

I didn't really think encapsulation existed in JavaScript, but a superior pointed out to me that it does exist and recommended JavaScript: The Good Parts by Douglas Crockford as a good read.

No comments:

Post a Comment