Objects
An object is a self-contained collection of data. This data comes in to forms: properties and methods
- A property is a variable belonging to an object
- A method is a function that the object can invoke
In JavaScript you can create your own objects , called user-defined objects.
JavaScript also coms with a range of premade objects that you can use in your scipts. These are called
native objects .
You may have been seen objects in action. Array is an object.
Whenever you initialize an array using the new keyword, you are creating a new instance of the
objects.when you want to find out how many elements are in an array, you do so by using the length property.
eg : var beatles = new Array();
beatles.length;
Other native objects examples include Math and Date, both of which have very useful methods for dealing
with numbers and datas respectively.
For example :
var num = 7.561;
var num = Math.round(num) ;
The Date object can be used to store and retrive infomation about a specific date and time.
If you create a new instance of the Date object, it will be automatically be prefilled with the current date
and time : eg :
var current_date = new Date();
var today = current_date.getDay();
Host objects
Native objects arent the only kind of premade objects that you can use in your scripts.
Another kind of objects supplied not by the JavaScript language itself, but by the environment in
which its running. In the case of Web, the environment is the web browser.
Objects that are supplied by the web browser are called host objects.
Host objects include Form, Image, and Element. These objects can be used to get information
about forms, images, and form elements within a web pages.
So lets take a summary of the objects in JavaScript, There are three kinds of objects in JavaScript :
- User-defined objects created from scratch by the programmer.
- Native objects like Array, Math and Date, which are built in to JavaScript
- Host objects that are provided by the browser