Javascript Object oriented programming

August 22, 2012
  • JavaScript is a prototype-based language which contains no class statement. Instead JavaScript uses functions as classes. Defining a class is as easy as defining a function.
  • An object constructor/object constructor function is a function that’s used to define a new object. In this function, we declare the initial properties/methods of the new object, and usually assign them a pre-defined value.
  • To create an instance of an object, we use the keyword “new”, followed by an object constructor. We can either use JavaScript’s built-in constructors to create a native object, or we can build our own constructors to create a user-defined object.
  • Every object method has a variable – this – which refers to the current instance of that object from which the method is called.

Example

     function Computer(name) {
          this.name=name;
          this.getName=function() {
             return this.name;
          }
     }

     var computer1 = new Computer("Desktop-1");
     alert(computer1.getName());//alerts Desktop-1
     var computer2 = new Computer("Desktop-2");
     alert(computer2.getName());//alerts Desktop-2

References

http://www.sitepoint.com/oriented-programming-1/

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript


Javascript Closures

August 17, 2012

A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Closures


Javascript Module Pattern

August 8, 2012

Module Pattern – It stays out of the global namespace, provides publicly addressable API methods, and supports protected or “private” data and methods along the way.

http://www.yuiblog.com/blog/2007/06/12/module-pattern/