Monday, 12 August 2013

java script Objects

java script Objects

Objects:
Javascript is a object oriented language which allows the user to use predefined object (or) create user defined objects.
·         Object is a kind of data that contains properties and methods.
·         Properties are used to hold the values that provide the information about the object
·         Methods are the actions that are performed on the objects

There are two ways to create objects in Javascripts

1. Direct instance
2. using functions( constructors)
1. using Direct instance:

<html>
<head>
<title>User-defined objects</title>
<script type="text/javascript">
var book = new Object();   // Create the object
    book.subject = "html"; // Assign properties to the object
    book.author  = "Ram";
</script>
</head>
<body>
<script type="text/javascript">
   document.write("Book name is : " + book.subject + "<br>");
   document.write("Book author is : " + book.author + "<br>");
</script>
</body>
</html>

2. using constructors

<!object constructor>
<html>
<body>

<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
document.write(firstname + " is " + age + " years old.");
}

myFather=new person("vijay","kumar",50,"blue");
//document.write(myFather.firstname + " is " + myFather.age + " years old.");

</script>

</body>

<!-- http://improvejavascript.blogspot.in/-->
</html>


Types of Objects:

  • There are two types of objects are available
  • 1. User Defined Objects 
  • 2. Predefined Objects


In JavaScript almost everything is an object. Even primitive datatypes (except null and undefined) can be treated as objects.

  • Booleans can be objects or primitive data treated as objects
  • Numbers can be objects or primitive data treated as objects
  • Strings are also objects or primitive data treated as objects
  • Dates are always objects
  • Maths and Regular Expressions are always objects
  • Arrays are always objects
  • Even functions are always objects

1.       String object: string object can be used to perform some operations on the string
<html>
 <head>
  </head>
<body>
 <script type="text/javascript">
   var name="java script"
   document.write("name"+name+"<br/>");
   document.write("length:"+name.length+"<br/>");
   document.write("uppercase:"+name.toUpperCase()+"<br/>");
   document.write("Lowercase:"+name.toLowerCase()+"<br/>);
   document.write("ColoredText:"+name.fontcolor("red")+"<br/>");
   document.write("Index:"name.indexOf("a")+"<br/>");
   document.write("Last index:"+name.lastIndexOf("a")+"<br/>");
   document.write("replace:"+name.replace("java","Viz")+"<br/>");
</script>
</body>
</html>


1.       Date: this object is used to retrieve the date and time.
<html>
 <head>
  </head>
   <body>
     <script type="text/javascript">
      var date=new Date();
      document.write("date:"+date+"<br/>");
      document.write("day:"+date.getDate()+"<br/>");
      document.write("Month:"+date.getMonth()+"<br/>");
      document.write("Year:"+date.getYear()+"<br/>");
      document.write("Hours:"+date.getHours()+"<br/>");
      document.write("minutes:"+date.getMinutes()+"<br/>");
      document.write("seconds:"+date.getSeconds()+"<br/>");
   </script>
 </body>
</html>




3. Boolean:
 this object represents either true or false.


No comments:

Post a Comment