JavaScript is an Object Oriented Programming (OOP) language.
An OOP language allows you to define your own objects and make your own variable types.
Object Oriented Programming
JavaScript is an Object Oriented Programming (OOP) language. An OOP language allows you to define your own objects and make your own variable types.
However, creating your own objects will be explained later, in the Advanced JavaScript section. We will start by looking at the built-in JavaScript objects, and how they are used. The next pages will explain each built-in JavaScript object in detail.
Note that an object is just a special kind of data. An object has properties and methods.
Properties
Properties are the values associated with an object.
In the following example we are using the length property of the String object to return the number of characters in a string:
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
The output of the code above will be:
12
Methods
Methods are the actions that can be performed on objects.
In the following example we are using the toUpperCase() method of the String object to display a text in uppercase letters:
<script type="text/javascript">
var str="Hello world!"
document.write(str.toUpperCase())
</script>
The output of the code above will be:
HELLO WORLD!
Object Creation
Generally objects may be created using the following syntax:
name = new Object()
For instance Array, Date, Number, Boolean, and String objects can be created this way as in the example below. Most objects may be created this way except for the Math object which cannot be instantiated (an instance of it may not be created).
ratings = new Array(6,9,8,4,5,7,8,10)
var home = new String("Residence")
var futdate = new Date()
var num1 = new Number()
String objects may also be created as follows:
var string = "This is a test."
Object Template
Objects are created using templates. Templates to objects are like cookie cutters to cookies. Cookie cutters are used to create instances of cookies and object templates are used to create instances of objects. For now, the template is shown. The following creates an object, olist.
function olist(elements)
{
this.elements = elements
this.listitems = new Array(elements)
this.getItem = list_getItem
this.setItem = list_getItem
}
The methods list_setItem and list_getItem are written as follows:
function list_getItem(element)
{
return this.listitems(element)
}
function list_setItem(element, stringval)
{
this.listitems(element) - stringval
}
Creating the Object
var list1 = new olist(10)
Changing the Object
list1.setItem(0,"This is the first item in the list")
list1.setItem(1,"This is the second item in the list")
list1.setItem(2,"This is the third item in the list")
The Prototype property
The prototype property can be used to create new properties of created objects (whether they are user defined or system defined) as follows:
list1.prototype.type = "1"
The Function Constructor
Creation:
minus2 = new Function("x","return x-2")
Usage:
y = minus2(10)
The value of y is now 8.
Object use
When writing JavaScript, you are embedding the JavaScript in an object. These objects may have functions. An example function is the alert() function. It may be called as follows:
alert("An error occurred!")
However, the window object is the highest level JavaScript object, therefore the following code does the same thing:
window.alert("An error occurred!")
The following code using the "this" object declaration will perform the same:
this.alert("An error occurred!")
Object and Property Reference
Objects and their properties are normally referenced using a dot between each object or its property or method. as follows:
document.write("This is a test.")
However, a method called array notation, may be used. Array notation is required when the first character of a property name is a digit since the dot method may not be used in this case. The above example in array notation is:
document ["write"] ("This is a test.")
Levels of Objects
JavaScript has basically three types of objects which are:
Top level objects
Objects that are properties of other objects
Objects that are not properties of other objects
|