JavaScript Object Oriented Features
In this JavaScript tutorial you will learn about JavaScript Object Oriented Features, object type, object instantiation, using object properties, using object methods and achieving object instantiation.
Object-oriented capabilities make any language or application powerful. This section outlines the object oriented features supported by JavaScript and how to use them in programming process.
JavaScript is not a complete programming language but, rather, it is a scripting language. There are some object-oriented features that are not supported by JavaScript. JavaScript does not support the features of inheritance, information hiding, encapsulation and classification that are supported by other object-oriented programming languages. Lack of information hiding and lack of encapsulation features in JavaScript cause the methods and properties to only be accessible directly in JavaScript. Similarly, JavaScript does not support the inheritance or classification features. These features cannot be used for developing hierarchy of object types.
The concepts of object reusability and modular approach concept are used by JavaScript in a very limited way. JavaScript can be reused in the Internet only by using SRC attribute (discussed in the SCRIPT tag in an earlier section of this tutorial). Polymorphism, another important feature of Object-oriented programming, is supported by JavaScript using arguments array in function definition, which will be explained in detail in later sections. The lack of these features does not reduce the capabilities of JavaScript in any way. JavaScript has many other compensating features, making it the best scripting language for use in web and server side programming.
Object based Features Supported by JavaScript:
JavaScript supports various features related to object based language and JavaScript is sometimes referred to as an object-based programming language. The vital features which JavaScript supports related to object based are:
Object Type:
JavaScript supports the development of Object types and in this context JavaScript supports both predefined and user-defined objects. It is possible to assign objects of any type to any variable. It is possible to instantiate the defined object types to create object instances in JavaScript, which is a very powerful feature of Object based language. In this context, the limitation of Object type in JavaScript does not permit users to carry out type enforcement on Object types.
Object Instantiation:
To carry out the process of creating specific object instances available in JavaScript, users can make use of a new operator.
These two powerful, object-based features supported by JavaScript described above make this an object model language. In JavaScript, the object types are defined by properties and methods. Properties of Objects are used to access the data values contained in an object. Users can make use of the properties of JavaScript objects for editing as well as reading, depending on whether or not the object’s nature is read only. If the user wants to carry out functions on the object, this is achieved by using methods that make use of the object’s properties.
Using object based features (object properties and methods), in JavaScript:
To use Object Properties:
Syntax for making use of JavaScript object properties is by using the object name followed by dot and then the property name:
objectname.propertyname
To use Object Methods:
Syntax for making use of JavaScript object methods follows the same process as object properties with the difference being that the property name is replaced by the method name and any arguments. To access object methods in JavaScript, the programmer uses the object name followed by dot and then the method name with any arguments:
objectname.methodname(arguments)
How to achieve Object instantiation:
The new operator available feature is used to achieve Object instantiation.
General syntax for new operator for creating objects in JavaScript is as follows:
variablename=new objecttype(arguments)
In the above syntax, new refers to keyword and the objecttype(arguments) refer to the constructor. The number of arguments taken by constructors varies from no arguments to one or more than one, depending on the type of object.
An example of object type with constructor taking no arguments:
CD=new Date()
Date refers to the predefined JavaScript object type. The Date() constructor does not take any parameter and the above statement stores the current date and time to the variable CD.
It is also possible to use the same object type. For example, the Date() constructer taking
a specified date as the parameter and creating object instances for that specified date.
CDD=new Date(96,3,3)
In this example, instances are created for the specified Date March 3,1996.