0% found this document useful (0 votes)
23 views5 pages

Week 7 Codes

The document contains five HTML code examples demonstrating the use of JavaScript objects and classes. It covers creating objects, assigning properties and methods, and using object literals. Each code example illustrates different aspects of object-oriented programming in JavaScript.

Uploaded by

thembi malinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Week 7 Codes

The document contains five HTML code examples demonstrating the use of JavaScript objects and classes. It covers creating objects, assigning properties and methods, and using object literals. Each code example illustrates different aspects of object-oriented programming in JavaScript.

Uploaded by

thembi malinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Code 1 : Objects

<html>

<head><title>objects</title>

<script type = "text/javascript">

var person = new Object(); // Create an instance of the object

person.name = "Angie"; // Assign properties to the object

person.age = 11;

</script>

</head>

<body bgcolor="lightblue">

<script type = "text/javascript">

document.write("<b>The person is " + person.name + ".");

document.write("<br />She is " + person.age + ".");

</script>

</body>

</html>

Code 2 : Objects2

<html>

<head><title>Using objects</title>

<script type= "text/javascript">

var toy = new Object(); // Create the object

toy.name = "Lego"; // Assign properties to the object

toy.color = "red";

toy.shape = "rectangle";

toy.display=printObject; // Function name is assigned as a

// property of the object

function printObject(){

document.write("<b>The toy is a " + toy.name + ".<br>");


document.write("It is a " + toy.color + " " + toy.shape + ".<br />");

</script>

</head>

<body>

<script type = "text/javascript">

toy.display(); //Object method is called

toy.name = "block";

toy.color="blue";

toy.shape = "cube";

toy.display();

</script>

</body>

</html>

Code 3 : Class_Objects

<html>

<head><title>Class objects</title>

</head>

<body bgcolor="lightgreen">

<script type= "text/javascript">

function Module(){ // Create a Module class

this.module1 = "Web Development 2B"; // Properties

this.course = "Diploma IT2";

var mod = new Module; // Create new Module object

alert(mod.module1 + " in " + mod.course);

</script>

</body>

</html>
Code 4 : Class_Objects2

<html>

<head><title>Class objects 2</title>

<script type ="text/javascript">

function Book(title, author, publisher){ // Receiving parameters

this.pagenumber=0; // Properties

this.title = title;

this.author = author;

this.publisher = publisher;

this.nextpage = pageForward; // Create methods

this.prevpage = pageBackward;

function pageForward(){ // Functions to be used as methods

this.pagenumber++;

return this.pagenumber;

function pageBackward(){

this.pagenumber--;

return this.pagenumber;

</script>

</head>

<body>

<script type = "text/javascript">

var myBook = new Book("JavaScript: A Beginner's Guide","John Pollock", "McGraw-Hill Education" );


// Create new object

myBook.pagenumber=9; //Assign a page number

document.write(myBook.title + "<br>" + myBook.author + "<br>" + myBook.publisher +


"<br>Current page is " + myBook.pagenumber );

document.write("<br>Next Page: " );


document.write("<br />" + myBook.nextpage());// Move forward a page

document.write("<br />Previous Page: ");

document.write("<br />" + myBook.prevpage()); // Move back a page

</script>

</body>

</html>

Code 5: Object Literals

<html>

<head><title>Object Literals</title>

<script type = "text/javascript">

var Car = { // Create a Car object

make:undefined,

year:2006,

price:undefined,

owner:{

name:"Henry Lee"

},

dealer: "SF Honda",

display: function() {details="Make: "+Car.make+"\n";

details += "Dealer: "+Car.dealer+"\n";

details += "Year: "+Car.year+"\n";

details += "Price: $"+Car.price+"\n";

details += "Owner: "+

Car.owner.name+"\n";

alert(details);

};

</script>

</head>
<body>

<script type="text/javascript">

Car.make="Honda Civic"; // Assign value

Car.year=2009; // Update the year

Car.price=30000;

Car.display();

</script>

</body>

</html>

You might also like