Goodspeed

导航

JavaScript OOP Demo

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript OOP Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>

<body>
$Header:
/home/cvsroot/CidSoftDocs/Developing/Others/AboutExpertGroup_dev.htm,v
1.9 2003/03/13 21:49:43 wj Exp $<br />
$Revision: 1.1 $<br />
$Date: 2003/05/31 17:16:50 $<br />
$Author: cvs $
<h1>JavaScript OOP Demo</h1>
<script language="JavaScript" type="text/JavaScript">
// Map Demo
var customers = new Object();
customers["key01"] = "value01";
customers["key02"] = "value02";
customers["key03"] = "value03";
for (var key in customers) {
 document.writeln(key + " : " + customers[key] + "<br/>");
}

document.writeln("<br/>");

// Singleton Class Demo - the sole instance of a class
var singletonObject = {
 attribute : "attribute01",
 array : {},

 operation : function (parameter) {
     document.writeln("parameter is " + parameter + "<br/>");
     document.writeln("attribute is " + singletonObject.attribute +
"<br/>");

     singletonObject.array[0] = "value01";
     document.writeln("array is " + singletonObject.array[0] + "<br/>");
    },

 toString : function () {
     return "I'm the sole instance of Singleton Class.";
    }
};
singletonObject.operation("myParameter");

document.writeln("<br/>");

// Class Demo
// MyClass's constructor
function MyClass() {
 this.attribute = "attribute01";
 this.array = [];
}

// MyClass's property
MyClass.prototype.property = "default value";

// MyClass's setter and getter
MyClass.prototype.setProperty = function (value) {
 this.property = value;
};
MyClass.prototype.getProperty = function () {
 return this.property;
};

// MyClass's static field and method
MyClass.staticField = "staticFieldValue";
MyClass.getInstance = function () {
       document.writeln("<br/>" + "invoke MyClass.getInstance()" + "<br/>");
       return new MyClass();
      }

// new MyClass's instance
var myClass = new MyClass();
myClass.setProperty("myValue");
document.writeln("myClass info : " + myClass.attribute + " " +
myClass.getProperty() + "<br/>");
document.writeln(MyClass.staticField);
MyClass.getInstance();

document.writeln("<br/>");

// Extends Class Demo
function MyExtendsClass() {
 this.name = "accason";
}
MyExtendsClass.prototype = new MyClass();

var myExtendsClass = new MyExtendsClass();
document.writeln("myExtendsClass info : " + myExtendsClass.name + " " +
myExtendsClass.getProperty() + "<br/>");
</script>
</body>
</html>

posted on 2004-03-03 12:44  Goodspeed  阅读(784)  评论(0编辑  收藏  举报