Objects are useful to organise information
Properties
Thr syntax for accessing a property of an objects is :
objName.propName
You can add properties to an object by simply giving it a value. Assume that the personObj already exists - you can give it properties name firstname, lastname, age, and eyecolor as follows:
personObj.firstname="John"
personObj.lastname="Doe"
personObj.age=30
personObj.eyecolor="blue"
document.write(personObj.firstname)
The Code above will generate the following ouput:
John
Methods
An object can also contain methods:
You can call a method with the following syntax:
objName.methodName( )
Note: Parameters required for the method can be passed between the parentheses.
To call a method called sleep( ) or the personObj:
personObj.sleep ( )
Example
<html>
<body>
<script typ="text/javascript">
personObj.firstname="John"
personObj.lastname="Doe"
personObj.age=30
personObj.eyecolor="blue"
document.write(personObj.firstname + " is " + personObj.age + " years old.")
</script>
</body>
</html>
0 comments:
Post a Comment