here is a sample implementation, you can try to do AddNew,Delete:<Script Language=JavaScript>
function StringToRecordset(s)
{
var a = s.split(/;/);
this.recordCount = a.length;
if (this.recordCount > 0)
{
this.recordset = new Array(a.length); for (var i=0; i < a.length; i++)
{
this.recordset[i] = a[i].split(/,/);
} this.MoveFirst();
}
}function RecordsetToString()
{
var s= "";
if (this.recordset)
{
for (var i=0; i < this.recordset.length; i++)
s += ";"+this.recordset[i].join(","); if (s != "")
s = s.substring(1);
} return s;
}function GetItem(n)
{
if (!this.EOF())
return this.recordset[this.cursor][n];
else
return null;
}function SetItem(n,v)
{
if (!this.EOF())
this.recordset[this.cursor][n] = v;
}
function MyRecordset()
{
this.cursor = -1;
this.recordCount = 0;
this.recordset = null;
this.RecordCount = function() {return this.recordCount;}
this.StringToRecordset = StringToRecordset;
this.RecordsetToString = RecordsetToString;
this.MovePrevious = function() { if (this.recordCount > 0 && this.cursor >0) this.cursor--;};
this.MoveNext = function () {if (this.recordCount > 0 && this.cursor < this.recordCount) this.cursor++; };
this.MoveFirst = function() { if (this.recordCount >0) this.cursor = 0;};
this.MoveLast = function() { if (this.recordCount > 0) this.cursor = this.recordCount - 1;};
this.BOF = function() {if (this.recordCount > 0) return this.cursor == -1; else return true;}
this.EOF = function() { if (this.recordCount > 0) return this.cursor == this.recordCount; else return true;};
this.GetItem = GetItem;
this.SetItem = SetItem;
} var s="張三,男,湖南;李四,男,廣東;王娟,女,江西";
var myRs=new MyRecordset();
myRs.StringToRecordset(s);
while(!myRs.EOF())
{
var sValue=myRs.GetItem(0);
myRs.SetItem(0,sValue+"00");
myRs.MoveNext();
}
var sNew=myRs.RecordsetToString();
alert(sNew);
</Script>
function StringToRecordset(s)
{
var a = s.split(/;/);
this.recordCount = a.length;
if (this.recordCount > 0)
{
this.recordset = new Array(a.length); for (var i=0; i < a.length; i++)
{
this.recordset[i] = a[i].split(/,/);
} this.MoveFirst();
}
}function RecordsetToString()
{
var s= "";
if (this.recordset)
{
for (var i=0; i < this.recordset.length; i++)
s += ";"+this.recordset[i].join(","); if (s != "")
s = s.substring(1);
} return s;
}function GetItem(n)
{
if (!this.EOF())
return this.recordset[this.cursor][n];
else
return null;
}function SetItem(n,v)
{
if (!this.EOF())
this.recordset[this.cursor][n] = v;
}
function MyRecordset()
{
this.cursor = -1;
this.recordCount = 0;
this.recordset = null;
this.RecordCount = function() {return this.recordCount;}
this.StringToRecordset = StringToRecordset;
this.RecordsetToString = RecordsetToString;
this.MovePrevious = function() { if (this.recordCount > 0 && this.cursor >0) this.cursor--;};
this.MoveNext = function () {if (this.recordCount > 0 && this.cursor < this.recordCount) this.cursor++; };
this.MoveFirst = function() { if (this.recordCount >0) this.cursor = 0;};
this.MoveLast = function() { if (this.recordCount > 0) this.cursor = this.recordCount - 1;};
this.BOF = function() {if (this.recordCount > 0) return this.cursor == -1; else return true;}
this.EOF = function() { if (this.recordCount > 0) return this.cursor == this.recordCount; else return true;};
this.GetItem = GetItem;
this.SetItem = SetItem;
} var s="張三,男,湖南;李四,男,廣東;王娟,女,江西";
var myRs=new MyRecordset();
myRs.StringToRecordset(s);
while(!myRs.EOF())
{
var sValue=myRs.GetItem(0);
myRs.SetItem(0,sValue+"00");
myRs.MoveNext();
}
var sNew=myRs.RecordsetToString();
alert(sNew);
</Script>
對懂的人來說當然不難﹐但我做這份卷子前我還完全不知道怎樣自定義對象﹐怎樣定義方法屬性并調用。就象當你還不懂這個函數時execCommand你怎么知道怎樣怎樣做CSDN的文檔中心的那個編輯器呢?
this.name=name;
this.data=data;
this.record=data.split(';');
this.count=this.record.length; //this.EOF
this.cursor=0;
this.moveNext=moveNext;
this.movePrevious=MovePrevious;
this.moveLast=MoveLast;
this.moveFirst=MoveFirst;
this.addNew=AddNew;
this.EOF=EOF;
this.BOF=BOF;
this.getItem=getItem;
this.setItem=setItem;
}function moveNext(){
if(this.cursor<this.count)this.cursor++;
return false;
}
function movePrevious(){
if(this.cursor!=0)this.cursor--;
return false;
}
function BOF(){
if(this.cursor==0)return true;
return false;
}
function EOF(){
if(this.cursor==this.count)return true;
return false;
}
function moveLast(){
this.cursor=this.count;
}
function moveFirst(){
this.cursor=0;
}
function getItem(id){
var fields=this.record[id].split(',');
return fields;
}
function setItem(id){
var arg=this.setItem.arguments;
var fields=this.record[id].split(',');
for(i=1;i<arg.length;i++){
if(arg[i]!='')this.fields[i]=arg[i];//''就保留原值
}
var result='';
for(i=0;i<fields.length;i++) {
if(i!=fields.length-1)result+=fields[i]+',';
else result+=fields[i];
}
this.record[id]=result;
} function addNew(name,sex,address){
var arg=this.addNew.arguments;
var tmp='';
if(!name||name=='') tmp+='noName'+this.record.length+',';
if(!sex||sex=='') tmp+='male,';
if(!address||address=='') tmp+='unknown';
this.record[this.record.length]=tmp;
}你说的那个字符串与对象呼转的函数就是RS的实例和RS.data的引用
string to object:
newRS=new RS(yourstr);
object to string:
yourstr=newRS.data;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD><BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
sR="張三,男,湖南;李四,男,廣東;王娟,女,江西";
function RS(data){
this.data=data;
this.record=new Array;
this.record=data.split(';');
this.count=this.record.length; this.cursor=0;
this.moveNext=moveNext;
this.movePrevious=movePrevious;
this.moveLast=moveLast;
this.moveFirst=moveFirst;
this.addNew=addNew;
this.EOF=EOF;
this.BOF=BOF;
this.getItem=getItem;
this.setItem=setItem;
this.deleteItem=deleteItem;
}function moveNext(){
if(this.cursor<this.count)this.cursor++;
return false;
}
function movePrevious(){
if(this.cursor!=0)this.cursor--;
return false;
}
function BOF(){
if(this.cursor==0)return true;
return false;
}
function EOF(){
if(this.cursor==this.count)return true;
return false;
}
function moveLast(){
this.cursor=this.count;
}
function moveFirst(){
this.cursor=0;
}
function getItem(id){
var fields=this.record[id].split(',');
return fields;//返回数组而不是字符串
}
function setItem(id){
var arg=this.setItem.arguments;
var fields=this.record[id].split(',');
for(i=0;i<(arg.length-1);i++){
if(arg[i+1]!='')fields[i]=arg[i+1];//''就保留原值
}
var result='';
for(i=0;i<fields.length;i++) {
if(i!=fields.length-1)result+=fields[i]+',';
else result+=fields[i];
}
this.record[id]=result;
} function addNew(name,sex,address){
var arg=this.addNew.arguments;
var tmp='';
if(!name||name=='') tmp+='noName'+this.record.length+',';
if(!sex||sex=='') tmp+='male,';
if(!address||address=='') tmp+='unknown';
this.record[this.record.length]=tmp;
}
function deleteItem(id){
this.record.splice(id,1)
}
me=new RS(sR)
alert(me.getItem(1))
me.setItem(1,'llrock','male','beijing')
alert(me.getItem(1))
me.moveLast()
alert('EOF='+me.EOF()+':'+'BOF='+me.BOF());
me.moveFisrt();
me.deleteItem(me.curcor);
alert(me.getItem(me.cursor))
//-->
</SCRIPT>
</BODY>
</HTML>