· Javascript permit the creation of user defined object.
· A user defined object will also be associated with property and methods.
· After creation of such an object, any number of instance of this object can be created & used.
Ø Create a direct instance of user defined object
Syntax : variablename = new Object()
Syntax for define property : userdefinedobject.property =value;
Example
<html>
<head>
<script language="javascript">
var book = new Object();
book.title="PHP";
book.author ="Wrox";
document.write("Title : "+book.title+"<br/>");
document.write("Author : "+book.author);
</script>
</head>
</html>
Output
Title : PHP
Author : Wrox
Ø Create a template of an object
Example
<html>
<head>
<script language="javascript">
function subject(title,author,price)
{
this.title=title;
this.author=author;
this.price=price;
}
</script>
</head>
<body>
<script type="text/javascript">
var x = new subject("PHP","Wrox","500");
document.write("Title : "+x.title+"<br/>");
document.write("Author : "+x.author+"<br/>");
document.write("Price : "+x.price+"<br/>");
</script>
</body>
</html>
Output
Title : PHP
Author : Wrox
Price : 500
· The template is just a function. Inside the function you need to assign thingd to this.property name.
· Here “this” refer to the current object in focus.
· Once you have the template, you can create new instance of the object, like this
· X = new subject("PHP","Wrox","500");
No comments:
Post a Comment