Wednesday, August 1, 2012

Object-oriented JavaScript

Object-oriented JavaScript:
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has great functions. It is a multi-paradigm language, supporting object-oriented, interactive and functional programming style. JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari. As its scripting language so it’s lightweight programming language. It’s usually embedded directly into HTML pages. And it is an interpreted language means that scripts execute without preliminary compilation.
JavaScript was formalized in the ECMAScript language standard and is primarily used in the form of client-side JavaScript, implemented as part of a Web Browser in order to provide enhanced user interfaces. This enables programmatic access to computational objects within environment. JavaScript uses syntax influenced by that of C. It also has many names and naming conventions from Java. JavaScript and Java are different language. Java is frequently used to program games, mobile phones and other devices and also for websites. JavaScript is powerful language that runs inside a web browser. Its role is to provide access to different elements of the pages so that they can be removed or updated. For example, it can send request to Web Server to get more information and update specific part of page without reload the page.
And JavaScript perform such task by accessing the Document Object Model (DOM), a model or structure similar to a family tree. JavaScript is an event-driven language. There are some events triggered automatically like load event and there are some event triggered by user interaction like click, tap on touch-sensitive screen or mouseover and mouseout.
You will get more information about JavaScript from here.
Constructor Functions
In JavaScript syntax, a constructor function represents the class that contains the template from which new object instances are created. Constructor functions create and initialize (set the default state of) properties in the new objects. The difference between a constructor function and a method function is that a constructor function uses the special this keyword to represent a reference to the new object that is being initialized. A method function typically only performs some action on a given set of an object’s data.
The following example illustrates one way to create a Rectangle constructor function that could be used to initialize the height and width of new Rectangle objects:
function Rectangle(w, h) {

this.width = w;

this.height = h;

}
You can also create a constructor function by using function literal syntax. Function literal syntax provides the same functionality as the syntax used previously and is merely an alternative way to write the constructor.
Rectangle = function(w, h) {

this.width = w;

this.height = h;

}
Instance variables
Instance variables are any variables (properties) that are defined in a constructor function and are copied into each object instance of that constructor. All object instances have their own copies of instance variables. This means that if there are five object instances of a Circle class, there are five copies of each instance variable defined in the class. Because each object instance has its own copy of an instance variable, each object instance can assign a unique value to an instance variable without modifying the values of other copies of the instance variable. You can access instance variables directly from their containing object instances.
The following example defines four instance variables—make, model, color, and speed—in a constructor function. These four instance variables are available directly from all object instances of the Car constructor:
function Car(make, model, color) {

// define a Car class

this.make = make;

this.model = model;

this.color = color;

this.speed = 0;

}
The following object instance objCar contains all four instance variables. Although a value for the instance variable speed is not passed to the Car constructor, objCar still has a speed property whose initial value is 0 because the speed variable is defined in the Car constructor.
// objCar.make = "Subaru"

// objCar.model = "Forester",

// objCar.color = "silver"

// objCar.speed = 0

var objCar = new Car("Subaru", "Forester", "silver");
Instance methods
Instance methods are any methods that are accessible using an object instance. Object instances do not have their own copies of instance methods. Instead, instance methods are first defined as functions, and then properties of the constructor function’s prototype object are set to the function values. Instance methods use the keyword this in the body of the defining constructor function to refer to the object instance they are operating on. Although a given object instance does not have a copy of an instance method, you still access instance methods directly from their associated object instances.
// increase the speed of a Car

function Car_increaseSpeed(x) {

this.speed += x;

return this.speed;

}

Car.prototype.increaseSpeed = Car_increaseSpeed;
The following example defines a function named Car_increaseSpeed(). The function name is then assigned to theincreaseSpeed property of the Car class’s prototype object:
var objCar = new Car("Subaru", "Forester", "silver");

var newSpeed = objCar.increaseSpeed(50);
You can also create an instance method by using function literal syntax. Using function literal syntax eliminates the need to define a function and the need to assign a property name to the function name. The following example uses function literal syntax to define an increaseSpeed() method that contains the same functionality as the increaseSpeed() function defined previously:
// increase the speed of a Car

Car.prototype.increaseSpeed = function(x) {

this.speed += x;

return this.speed;

}
You will get more interesting information about OOP with JavaScript from the video tutorial give below:
JavaScript tutorial
Friends, lets share your ideas :)

No comments:

Post a Comment