Define Object, use its instance : Objects Object Oriented : Language Basics : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP


JavaScript DHTML  »  Language Basics   » [  Objects Object Oriented  ]   
 



Define Object, use its instance

Please note that some example is only working under IE or Firefox.

/*
JavaScript: A Beginner's Guide, Second Edition
By John Pollock

Publisher: McGraw-Hill, 

Published December 2003, 550 pages, 
ISBN 0072227907
*/

 
<HTML>

<HEAD>

<SCRIPT language="JavaScript">
<!-- 
function get_price(){
var the_price=1000;
 if(this.speed == "500mHz")
  the_price+=200;
 else
  the_price+=100;
 
 if(this.hdspace == "15GB")
  the_price+=50;
 else
  the_price+=25;
 
 if(this.ram == "128MB")
  the_price+=150;
 else
  the_price+=75;

  return the_price;
}

function computer(speed,hdspace,ram){
    this.speed=speed;
    this.hdspace=hdspace;
    this.ram=ram;
    this.price=get_price;
}

var work_computer= new computer("500mHz","15GB","128MB");
var home_computer= new computer("450mHz","10GB","64MB");
var laptop_computer= new computer("350mHz","7GB","32MB");

var work_computer_price= work_computer.price();
var home_computer_price= home_computer.price();
var laptop_computer_price= laptop_computer.price();

//-->
</SCRIPT>

</HEAD>

<BODY>

<SCRIPT language="JavaScript">
<!-- 

document.write("<H2>The information on the computers you requested:</H2>");
document.write("<B>Work Computer: </B>");
document.write(work_computer.speed+","+work_computer.hdspace+","+work_computer.ram);
document.write("<BR>");
document.write("<B>Price:</B> $"+work_computer_price);
document.write("<P>");
document.write("<B>Home Computer: </B>");
document.write(home_computer.speed+","+home_computer.hdspace+","+home_computer.ram);
document.write("<BR>");
document.write("<B>Price:</B> $"+home_computer_price);
document.write("<P>");
document.write("<B>Laptop Computer: </B>");
document.write(laptop_computer.speed+","+laptop_computer.hdspace+","+laptop_computer.ram);
document.write("<BR>");
document.write("<B>Price:</B> $"+laptop_computer_price);

//-->
</SCRIPT>

</BODY>

</HTML>
           
       
Related examples in the same category
1.  Object utility: create, parse and profile
2.  Define and use object
3.   Creating an Object and Using Object Instance Properties and Methods
4.   Object-Oriented Planetary Data Presentation
5.  The Book Object Definition
6.  Complete Example of Using the employee, client, and project Objects
7.  Source Code for the showBook() Example
8.  Using the Book Object Constructor
9.  Creating Objects Dynamically
10.  Object to array
11.  Utility class for JavaScript class definitionHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.