JAVASCRIPT OBJECTS INTRODUCTION

JavaScript is an Object Oriented Programming (OOP) language. A programming language can be called object-oriented if it provides four basic capabilities to developers:
  • Encapsulation . the capability to store related information, whether data or methods, together in an object
  • Aggregation . the capability to store one object inside of another object
  • Inheritance . the capability of a class to rely upon another class (or number of classes) for some of its properties and methods
  • Polymorphism . the capability to write one function or method that works in a variety of different ways
Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object otherwise, the attribute is considered a property.
Object Properties:
Object properties can be any of the three primitive data types, or any of the abstract data types, such as another object. Object properties are usually variables that are used internally in the object's methods, but can also be globally visible variables that are used throughout the page.
 syntax for adding a property to an object is:
objectName.objectProperty = propertyValue;
Example:
 example to show how to get a document title using "title" property of document object:
var str = document.title;
Object Methods:
The methods are functions that let the object do something or let something be done to it. A method is attached to an object and can be referenced by the this keyword.
Methods are useful for everything from displaying the contents of the object to the screen to performing complex mathematical operations on a group of local properties and parameters.
Example:
simple example to show how to use write() method of document object to write any content on the document:

document.write("This is a table");

No comments:

Post a Comment