Quick helper links…………….

Important links……


Basic about Object Oriented JavaScript

JavaScript has nice and special object-oriented features. Discussion about some of these features are…….

(1) JavaScript Object

Object in JavaScript is not same as C#, Java. JavaScript object is called a name-value pair i.e just like {“name” : “value”}. A function in JavaScript is basically an object function. Example…..

  • var motor = { “Name” : “Honda” };
  • alert( motor.Name ); // output is Honda.

(2)Instantiating Object

In JavaScript an object is instantiated by calling function with the new operator. This is done by following way…..

  • function Car(name) {
  • this.name=name;
  • }
  • var motor = new Car(“Honda”);

(3) Prototype object

An object is a copy of existing example (prototype) object. Any properties and method of the prototype object also appear in the created object. That means created object inherits the properties and method of the prototype object. Every constructor function/object has property name “prototype” and this prototype object has a property called “constructor”. Constructor property indicates the base constructor function. One thing need to remember that “constructor” is not the own property of created object. In JavaScript “Object.prototype” is the ultimate base prototype of  all prototypes.

(4)Static members

A member declared in a function object  associated with the function name is called static member of the object. The calling of the static member/function is same as Java.

  • function Car() {
  • Car.name= function(){
  • return “Motor”;
  • };
  • }
  • Car.name();

(5)Concept of closure

Anonymous inner function is treated as closure in JavaSript because the inner function closes over the environment (the arguments and local variables of the outer function) in which it is defined. Closure in JavaScript is used for simulating private members. See the following….

  • function Check (upperbound) {
  • return function (lowerbound ) {
  • return (upperbound>lowerbound) ? true : false;
  • };
  • }
  • var bigger =  Check (100);
  • alert(bigger(90));  // return true.

(6) Accessing Private Members

Normally local variables of a function can’t be accessed outside the function. But in JavaScript local variable can be accessed through inner function’s closure.

(7) Inheriting classes

Properties of one class(prototype) is inherited by another class(prototype) only by constructor function. Lets see it in following…..

  • function vehicle {
  • //
  • }
  • function car {
  • //
  • }
  • car.prototype = new vehicle();

Here, car inherits the vehicle class.

(8) Namespaces

Namespaces in JavaScript is used for creating JavaScript library. In the following way we can build Namespaces for prototype classes…..

  • var namespace={ };
  • namespace.ClassPrototype = function() {
  • //
  • };

Read the rest of this entry »