title: Encapsulation in Javascript
body: <p>I'm pretty new to Javascript, as my SO profile will attest.</p>
  
  <p>I've just been reading up on a few tutorials and come across something I don't totally understand in regards to Object Orientation and Encapsulation when applied with Javascript.</p>
  
  <p>The tutorial stated that Javascript objects can be declared like this:</p>
  
  <pre><code>var myCustomObject = new Object();
  </code></pre>
  
  <p>And that you can give it instance variables like this:</p>
  
  <pre><code>myCustomObject.myVariable = "some value";
  myCustomObject.myOtherVariable = "deadbeef";
  </code></pre>
  
  <p>Finally, it states that you can create a template function to create new objects like this:</p>
  
  <pre><code>function CustomObject(myVariable, myOtherVariable)
  {
      this.myVariable = myVariable;
      this.myOtherVariable = myOtherVariable;
  }
  </code></pre>
  
  <p>I also know that you can create and assign values to variables that do not yet exist and as a result are declared implicitly, as is seen in the example, where <code>myCustomObject</code> didn't have a <code>myVariable</code>, but now it does.</p>
  
  <p>So, my question is: What is there to prevent new variables from being added at some other point in the code. If I'm trying to learn how an object works and what I can/should do with it, I may never see the variable additions that could well be in some other .js file, and thus not have a full understanding of the object...</p>
  
  <p>Also, how do I know that some object that has just been created won't suddently turn out to have 60 more variables added later on in code that weren't mentioned at all at creation time?</p>
  
  <p>How are you meant to be able to understand what an object can contain at a glance if more can just be added to it "willy nilly"?</p>