Object orientation in Javascript

I realise I am just echoing what Doug Crockford wrote nearly 10 years ago - It’s still little known though so I thought I’d throw it out there.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="FooManChoo"></div>
<script type="text/javascript">
$ = function(id) { return document.getElementById(id);};

var Foo = function(id, options)  {
	  
	  // private variables
      var elm = document.getElementById(id),
	  favouriteBeer = 'Pale Ale';
	  
	  elm.Foo = this; // reference to object so you can access public properties
	  
	  // public variables
      this.beer = false;
	  
	  // private function
      function bar() { alert('Private Bar stocks ' + favouriteBeer); }
	  
	  // public function
	  this.bar = function() {
	  	bar();
	  	if (this.beer) {
	  		alert('Public bar has Beer :) !');
		}
		else {
	  		alert('Public bar has no Beer :(');
		}
	  }
};

new Foo('FooManChoo');
$('FooManChoo').Foo.bar();
$('FooManChoo').Foo.beer = true;
$('FooManChoo').Foo.bar();

</script>
</body>
</html>

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

javascript is awesome.