<html> <head> <title>JavaScript</title> <script>// Create a shopping cart.var cart = new ShoppingCart();/****************************************************************************** * 定义产品 Product 对象 * ******************************************************************************/// Constructor for the Product object.function Product(name, num, price, shipping, quantity) { this.name = name; this.catalogNumber = num; this.price = price; this.shippingCost = shipping; this.quantity = quantity; this.totalCost = totalCost; }// 计算总价值的方法 totalCostfunction totalCost() { return ((this.price + this.shippingCost) * this.quantity); }/****************************************************************************** * 定义购物车 ShoppingCart 对象 * ******************************************************************************/// 构造对象函数 ShoppingCartfunction ShoppingCart() { this.items = new Array(); this.addItem = addItem; this.removeItem = removeItem; }// 添加产品项的方法 addItemfunction addItem(item) { var i; // See if the item already exists in the list, if so then increment the quantity. for (i = 0; i < this.items.length; i++) if (this.items[i].catalogNumber == item.catalogNumber) { this.items[i].quantity++; return; } // Otherwise, add it to the end of the list. this.items[this.items.length] = item; }// 删除产品项的方法 removeItemfunction removeItem(catno) { var i, j; var tmp; // See if the item already exists in the list, if so then decrement the quantity. for (i = 0; i < this.items.length; i++) if (this.items[i].catalogNumber == catno) { this.items[i].quantity--; if (this.items[i].quantity > 0) return; // If the quantity drops to zero, remove the item from the list. This is done by // making a copy the array minus the item to be deleted. tmp = new Array(); for (j = 0; j < i; j++) tmp[tmp.length] = this.items[j]; for (j = i + 1; j < this.items.length; j++) tmp[tmp.length] = this.items[j]; this.items = null; this.items = tmp; } }/****************************************************************************** * 对象定义结束 * ******************************************************************************/// 此函数在下边的帧中显示购物车以及购物车上的产品function displayCart() { var i, s; var total; parent.frames["B"].document.open("text/html", "replace"); parent.frames["B"].document.writeln('<html><head><body bgcolor="#ffffff">'); parent.frames["B"].document.writeln('<font face="Arial,Helvetica" size=3>'); parent.frames["B"].document.writeln('<b>购物车:</b>'); parent.frames["B"].document.writeln('<p>'); parent.frames["B"].document.writeln('<table border=1 cellpadding=2 cellspacing=0>'); s = '<th><font face="Arial,Helvetica" size=3>产品名称</font></th>' + '<th><font face="Arial,Helvetica" size=3>产品编号</font></th>' + '<th><font face="Arial,Helvetica" size=3>价格</font></th>' + '<th><font face="Arial,Helvetica" size=3>运费</font></th>' + '<th><font face="Arial,Helvetica" size=3>数量</font></th>' + '<th><font face="Arial,Helvetica" size=3>总价值</font></th>'; parent.frames["B"].document.writeln(s); total = 0; for (i = 0; i < cart.items.length; i++) { parent.frames["B"].document.writeln('<tr>'); s = '<td><font face="Arial,Helvetica" size=3>' + cart.items[i].name + '</font></td>' + '<td><font face="Arial,Helvetica" size=3>' + cart.items[i].catalogNumber + '</font></td>' + '<td><font face="Arial,Helvetica" size=3>' + format(cart.items[i].price) + '</font></td>' + '<td><font face="Arial,Helvetica" size=3>' + format(cart.items[i].shippingCost) + '</font></td>' + '<td><font face="Arial,Helvetica" size=3>' + cart.items[i].quantity + '</font></td>' + '<td><font face="Arial,Helvetica" size=3>' + format(cart.items[i].totalCost()) + '</font></td>'; parent.frames["B"].document.writeln(s); total += cart.items[i].totalCost(); } parent.frames["B"].document.writeln('</table>'); parent.frames["B"].document.writeln('<p>'); parent.frames["B"].document.writeln('<b>总金额:</b> ' + format(total)); parent.frames["B"].document.writeln('</font>'); parent.frames["B"].document.writeln('</body></html>'); parent.frames["B"].document.close(); }// This function formats numbers as dollars and cents.function format(n) { var s = "$"; var d, c; d = parseInt(n, 10); c = Math.round((n - d) * 100); if (c == 0) c = "00"; else if (c < 9) c = "0" + c; return("$" + d + "." + c); }</script> </head> <body bgcolor="#ffffff"> <b><font size=2>用来说明自定义对象的一个简单购物车的例子: </font> </b> <p> <font size=2>请选择您想要购买的产品: </font> <font face="Arial,Helvetica" size=2> <p> </font> <form> <table border=0 cellpadding=4 cellspacing=0> <tr bgcolor="#999999" valign=bottom> <th><font size="3">产品项</font></th> <th><font size="3">产品编号</font></th> <th><font size="3">价格</font><font size=2><br></font><font face="Arial,Helvetica" size=2>(</font><font size="3">运费和加工费</font><font face="Arial,Helvetica" size=3>)</font></th> <td> </td> <td> </td> </tr> <tr bgcolor="#cccccc" valign=top> <td><font face="Arial,Helvetica" size=2>Dehydrator</font></td> <td><font face="Arial,Helvetica" size=2>9203-7</font></td> <td><font face="Arial,Helvetica" size=2>$139.95 ($7.25)</font></td> <td><input type="button" value="增加" onClick="cart.addItem(new Product('Dehydrator', '9203-7', 139.95, 7.50, 1)); displayCart();"></td> <td><input type="button" value="删除" onClick="cart.removeItem('9203-7'); displayCart();"></td> </tr> <tr bgcolor="#999999" valign=top> <td><font face="Arial,Helvetica" size=2>Blender</font></td> <td><font face="Arial,Helvetica" size=2>9003-2</font></td> <td><font face="Arial,Helvetica" size=2>$24.95 ($2.50)</font></td> <td><input type="button" value="增加" onClick="cart.addItem(new Product('Blender', '9003-2', 24.95, 2.50, 1)); displayCart();"></td> <td><input type="button" value="删除" onClick="cart.removeItem('9003-2'); displayCart();"></td> </tr> <tr bgcolor="#cccccc" valign=top> <td><font face="Arial,Helvetica" size=2>Juicer</font></td> <td><font face="Arial,Helvetica" size=2>9117-1</font></td> <td><font face="Arial,Helvetica" size=2>$49.95 ($5.00)</font></td> <td><input type="button" value="增加" onClick="cart.addItem(new Product('Juicer', '9117-1', 49.95, 5.00, 1)); displayCart();"></td> <td><input type="button" value="删除" onClick="cart.removeItem('9117-1'); displayCart();"></td> </tr> </table> <p> </form> <p><!-- This little snippet of code let's the user view the source for the page. --><form onSubmit="location.href = 'view-source:' + location; return false;"> <input type="submit" value="查看源代码!"> </form></body> </html>
总结一下: function MyObj(value1,value2){ var v1,v2; this.v1 = value1; //定义属性 this.v2 = value2; this.fnAdd = fnAdd; //定义方法 } function fnAdd(){ } 使用这个对象的时候是这样的, function UseObj(){ var newObj = new MyObj(v1,v2) }请问各位还有什么补充?
<input name=a>
<input type=button value=ok onclick="myForm.post();">
</form>
<script>
function MyForm(obj)
{
this.thisForm = obj;
this.post=postfun
function postfun()
{
alert('已经将您的设置发送。共有元素'+this.thisForm.elements.length+'个');
return true;
}
}
var myForm=new MyForm(document.frm);
</script>
<html>
<head><title>計算例子</title>
<body>
<script src="compute.js"></script>
<script language="JavaScript">function fnCompute(inSym)
{
var v1 = document.all.value1.value;
var v2 = document.all.value2.value;
var strResult,strSym;
if(v1=="") v1 = 0;
if(v2=="") v2 = 0;
var compute = new clsCompute(v1,v2);
switch(inSym){
case "add":
strSym = "+";
strResult = compute.fnAdd();
break;
case "sub":
strSym = "-";
strResult = compute.fnSub();
break;
case "div":
if(v2==0) {
alert("error");
return;
}
strSym = "/";
strResult = compute.fnDiv();
break;
case "mul":
strSym = "*";
strResult = compute.fnMul();
break;
default:
return;
break;
}
document.all.result.value = strResult;
document.all.sym.innerText = strSym;
}</script>
<table>
<input type="text" name="value1">
<span id="sym">+</span>
<input type="text" name="value2">=
<input type="text" id="result">
<br>
<input type="button" name="add" value=" add " onclick="fnCompute('add')">
<input type="button" name="sub" value=" sub " onclick="fnCompute('sub')">
<input type="button" name="div" value=" div " onclick="fnCompute('div')">
<input type="button" name="mul" value=" mul " onclick="fnCompute('mul')">
<input type="button" value=" TestSpace " onclick="fnTest()">
</table>
</body>
</html>
下面是compute.js文件..
function clsCompute(value1,value2){
var v1,v2;
this.v1 = parseInt(value1);
this.v2 = parseInt(value2);
this.fnAdd = fnAdd;
this.fnSub = fnSub;
this.fnDiv = fnDiv;
this.fnMul = fnMul;
}
function fnAdd(){
return this.v1+this.v2;
}
function fnSub(){
return this.v1-this.v2;
}
function fnDiv(){
return this.v1/this.v2;
}
function fnMul(){
return this.v1*this.v2;
}
用js只能實現數據和函數的封裝.有點像c中的結構!
function MYOBJ(name,age,sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.toString = function(){
return "姓名:"+ this.name +"年龄:"+ this.age +"性别:" + this.sex
};
}
//在程序中便可以直接使用该对象,及其对象的属性和方法
var obj = new MYOBJ("ballack",26,"male");
alert(obj.toString());
alert(typeof(obj));
alert(obj instanceof MYOBJ);
alert(obj.constructor == MYOBJ);</script>
/////////////////////////////////////By runmin///////////////////////////////////
//*************global vars**************var charc = new Array();var flg = 0;for (var i=0;i<256;i++)
{
if ( i >= flg*26+26 ) flg++; if (flg==0)
charc[i] = String.fromCharCode(i-(flg*26)+65);
else
charc[i] = String.fromCharCode(flg+64)+String.fromCharCode(i-(flg*26)+65);
}//*cls Convertor*creator****************
function Convertor()
{
this.HTMLData; // Source Table Id this.startX = 0; // Excel Start X Position
this.startY = 0; // Excel Start Y Position this.showApp = true; // Show Office Application
this.lineStyle = 1; // LineStyle this.ExpToExcel = ExpToExcel; // ExpToExcel Method
this.ExpToWord = ExpToWord; // ExpToWord Method
}//*cls Convertor*Exp method(To Excel)***
function ExpToExcel()
{
var oExcel = new ActiveXObject("Excel.Application");
var oBook = oExcel.Workbooks.Add(); // Create WorkSheet
var oSheet = oBook.Worksheets(1); var srcTRowlen = this.HTMLData.rows.length; // Source Table Rows length var srcTCollen = this.HTMLData.rows[0].cells.length-1; // Source Table Cols length // Add Cells
for (var i=0;i<this.HTMLData.rows.length;i++)
{
for (var j=0;j < this.HTMLData.rows[i].cells.length;j++)
{
oSheet.Range(charc[j+this.startX]+""+(i+1+this.startY)).Value = this.HTMLData.rows[i].cells[j].innerText;
}
}; // Set CellStyle
with (oSheet.Range(charc[this.startX]+""+(1+this.startY)+":"+charc[srcTCollen+this.startX]+""+(srcTRowlen+this.startY)))
{
Borders.LineStyle = this.lineStyle;
EntireColumn.AutoFit();
} oExcel.Visible = this.showApp;
// oExcel.SaveWorkspace("c:\\tmp.xls");
// oExcel.Workbooks.Item(1).PrintOut();// oExcel.Quit();
}//*cls Convertor*Exp method(To Word)****
function ExpToWord()
{
var oWord = new ActiveXObject("Word.Application");
var oDocument = new ActiveXObject("Word.Document"); oDocument = oWord.Documents.Add(); // Create Document // Add blank Table
oDocument.Tables.Add(oDocument.Range(0,0),this.HTMLData.rows.length,this.HTMLData.rows[0].cells.length); // Add Table Content
for (var i=0;i<this.HTMLData.rows.length;i++)
{
for (var j=0;j<this.HTMLData.rows[i].cells.length;j++)
{
oDocument.Tables(1).Rows(i+1).Cells(j+1).Range.Text = this.HTMLData.rows[i].cells[j].innerText;
}
} // Set CellStyle oDocument.Tables.Item(1).Borders.InsideLineStyle = this.lineStyle;
oDocument.Tables.Item(1).Borders.OutsideLineStyle = this.lineStyle;
oDocument.Tables.Item(1).Rows.Item(1).Range.Bold = true; oWord.Visible = this.showApp;
}/////////////////////////////////////////////////////////////////////////////////写的不好,见笑了:)
<xml id=flashData src="http://www.chinatools.biz:9009/flashbook/result.xml"></xml>
<table id=data datasrc="#flashData">
<thead>
<TR>
<TH>标题</TH>
<TH>作者</TH>
<TH>时间</TH>
</TR>
</thead>
<tbody datafld="dataroot">
<TR>
<TD><div datafld="topic"></div></TD>
<TD><div datafld="UserName"></div></TD>
<TD><div datafld="dateandtime"></div></TD>
</TR>
</tbody>
</TABLE>
<script>
function exp()
{
var tmp = new Convertor();
tmp.HTMLData = document.all.tags("table")[0];
tmp.ExpToWord();
}
</script>
</BODY>
<head>
<title>JavaScript</title>
<script>// Create a shopping cart.var cart = new ShoppingCart();/******************************************************************************
* 定义产品 Product 对象 *
******************************************************************************/// Constructor for the Product object.function Product(name, num, price, shipping, quantity) { this.name = name;
this.catalogNumber = num;
this.price = price;
this.shippingCost = shipping;
this.quantity = quantity;
this.totalCost = totalCost;
}// 计算总价值的方法 totalCostfunction totalCost() { return ((this.price + this.shippingCost) * this.quantity);
}/******************************************************************************
* 定义购物车 ShoppingCart 对象 *
******************************************************************************/// 构造对象函数 ShoppingCartfunction ShoppingCart() { this.items = new Array();
this.addItem = addItem;
this.removeItem = removeItem;
}// 添加产品项的方法 addItemfunction addItem(item) { var i; // See if the item already exists in the list, if so then increment the quantity. for (i = 0; i < this.items.length; i++)
if (this.items[i].catalogNumber == item.catalogNumber) {
this.items[i].quantity++;
return;
} // Otherwise, add it to the end of the list. this.items[this.items.length] = item;
}// 删除产品项的方法 removeItemfunction removeItem(catno) { var i, j;
var tmp; // See if the item already exists in the list, if so then decrement the quantity. for (i = 0; i < this.items.length; i++)
if (this.items[i].catalogNumber == catno) {
this.items[i].quantity--;
if (this.items[i].quantity > 0)
return; // If the quantity drops to zero, remove the item from the list. This is done by
// making a copy the array minus the item to be deleted. tmp = new Array();
for (j = 0; j < i; j++)
tmp[tmp.length] = this.items[j];
for (j = i + 1; j < this.items.length; j++)
tmp[tmp.length] = this.items[j];
this.items = null;
this.items = tmp;
}
}/******************************************************************************
* 对象定义结束 *
******************************************************************************/// 此函数在下边的帧中显示购物车以及购物车上的产品function displayCart() { var i, s;
var total; parent.frames["B"].document.open("text/html", "replace");
parent.frames["B"].document.writeln('<html><head><body bgcolor="#ffffff">');
parent.frames["B"].document.writeln('<font face="Arial,Helvetica" size=3>');
parent.frames["B"].document.writeln('<b>购物车:</b>');
parent.frames["B"].document.writeln('<p>');
parent.frames["B"].document.writeln('<table border=1 cellpadding=2 cellspacing=0>');
s = '<th><font face="Arial,Helvetica" size=3>产品名称</font></th>' +
'<th><font face="Arial,Helvetica" size=3>产品编号</font></th>' +
'<th><font face="Arial,Helvetica" size=3>价格</font></th>' +
'<th><font face="Arial,Helvetica" size=3>运费</font></th>' +
'<th><font face="Arial,Helvetica" size=3>数量</font></th>' +
'<th><font face="Arial,Helvetica" size=3>总价值</font></th>';
parent.frames["B"].document.writeln(s); total = 0;
for (i = 0; i < cart.items.length; i++) {
parent.frames["B"].document.writeln('<tr>');
s = '<td><font face="Arial,Helvetica" size=3>' + cart.items[i].name + '</font></td>' +
'<td><font face="Arial,Helvetica" size=3>' + cart.items[i].catalogNumber + '</font></td>' +
'<td><font face="Arial,Helvetica" size=3>' + format(cart.items[i].price) + '</font></td>' +
'<td><font face="Arial,Helvetica" size=3>' + format(cart.items[i].shippingCost) + '</font></td>' +
'<td><font face="Arial,Helvetica" size=3>' + cart.items[i].quantity + '</font></td>' +
'<td><font face="Arial,Helvetica" size=3>' + format(cart.items[i].totalCost()) + '</font></td>';
parent.frames["B"].document.writeln(s);
total += cart.items[i].totalCost();
} parent.frames["B"].document.writeln('</table>');
parent.frames["B"].document.writeln('<p>');
parent.frames["B"].document.writeln('<b>总金额:</b> ' + format(total));
parent.frames["B"].document.writeln('</font>');
parent.frames["B"].document.writeln('</body></html>');
parent.frames["B"].document.close();
}// This function formats numbers as dollars and cents.function format(n) { var s = "$";
var d, c; d = parseInt(n, 10);
c = Math.round((n - d) * 100);
if (c == 0)
c = "00";
else if (c < 9)
c = "0" + c;
return("$" + d + "." + c);
}</script>
</head>
<body bgcolor="#ffffff">
<b><font size=2>用来说明自定义对象的一个简单购物车的例子:
</font>
</b>
<p>
<font size=2>请选择您想要购买的产品:
</font>
<font face="Arial,Helvetica" size=2>
<p>
</font>
<form>
<table border=0 cellpadding=4 cellspacing=0>
<tr bgcolor="#999999" valign=bottom>
<th><font size="3">产品项</font></th>
<th><font size="3">产品编号</font></th>
<th><font size="3">价格</font><font size=2><br></font><font face="Arial,Helvetica" size=2>(</font><font size="3">运费和加工费</font><font face="Arial,Helvetica" size=3>)</font></th>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#cccccc" valign=top>
<td><font face="Arial,Helvetica" size=2>Dehydrator</font></td>
<td><font face="Arial,Helvetica" size=2>9203-7</font></td>
<td><font face="Arial,Helvetica" size=2>$139.95 ($7.25)</font></td>
<td><input type="button" value="增加" onClick="cart.addItem(new Product('Dehydrator', '9203-7', 139.95, 7.50, 1)); displayCart();"></td>
<td><input type="button" value="删除" onClick="cart.removeItem('9203-7'); displayCart();"></td>
</tr>
<tr bgcolor="#999999" valign=top>
<td><font face="Arial,Helvetica" size=2>Blender</font></td>
<td><font face="Arial,Helvetica" size=2>9003-2</font></td>
<td><font face="Arial,Helvetica" size=2>$24.95 ($2.50)</font></td>
<td><input type="button" value="增加" onClick="cart.addItem(new Product('Blender', '9003-2', 24.95, 2.50, 1)); displayCart();"></td>
<td><input type="button" value="删除" onClick="cart.removeItem('9003-2'); displayCart();"></td>
</tr>
<tr bgcolor="#cccccc" valign=top>
<td><font face="Arial,Helvetica" size=2>Juicer</font></td>
<td><font face="Arial,Helvetica" size=2>9117-1</font></td>
<td><font face="Arial,Helvetica" size=2>$49.95 ($5.00)</font></td>
<td><input type="button" value="增加" onClick="cart.addItem(new Product('Juicer', '9117-1', 49.95, 5.00, 1)); displayCart();"></td>
<td><input type="button" value="删除" onClick="cart.removeItem('9117-1'); displayCart();"></td>
</tr>
</table>
<p>
</form>
<p><!-- This little snippet of code let's the user view the source for the page. --><form onSubmit="location.href = 'view-source:' + location; return false;">
<input type="submit" value="查看源代码!">
</form></body>
</html>
function MyObj(value1,value2){
var v1,v2;
this.v1 = value1; //定义属性
this.v2 = value2;
this.fnAdd = fnAdd; //定义方法
}
function fnAdd(){
}
使用这个对象的时候是这样的,
function UseObj(){
var newObj = new MyObj(v1,v2)
}请问各位还有什么补充?
可以function MyObj(value1,value2){
var v1,v2;
this.v1 = value1; //定义属性
this.v2 = value2;
this.fnAdd = fnAdd; //定义方法
function fnAdd(){
}
}function fnAdd就是private了