topical media & game development

talk show tell print

#javascript-code-23-private.js / js



  // A Object constructor that represents a classroom
  function Classroom( students, teacher ) {
      // A private method used for display all the students in the class
      function disp() {
          alert( this.names.join(", ") );
      }
  
      // Store the class data as public object properties
      this.students = students;
      this.teacher = teacher;
  
      // Call the private method to display the error
      disp();
  }
  
  // Create a new classroom object
  var class = new Classroom( [ "John", "Bob" ], "Mr. Smith" );
  
  // Fails, as disp is not a public property of the object
  class.disp();
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.