如何用javascript写一个类,实现面向对象的效果?

时间:2021-05-20 19:51:34
写成如下形式
MyClass()
{
     属性1;
     属性2;
     。。。。。。
     方法1();
     方法2();
}

这些javascript能作到么?
怎么做?

9 个解决方案

#1


http://blog.csdn.net/gztoby/archive/2004/08/13/74248.aspx

#2


http://www.google.com/search?hl=zh-CN&q=javascript+%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1&lr=

可以

#3


net_lover(孟子E章),是我偶像,呵呵

#4


function MyClass()
{
    this.Property1 = "";
    this.Property2 = "";

    this.Method1 = function()
    {
    }
}

#5


我写了一个类,但他说有错误:‘strMsg’未定义,为甚么?
<script>
function Alert(strMsg)
{
this.strMsg = strMsg;
this.showMsg = showMsg;
function showMsg()
{
alert(strMsg);
}
}
</script>
<script>
var alertObj = new Alert("hehehehe");
document.onclick = alertObj.showMsg; 
</script>

#6


发错了,是这个有错误,上面的那个没错
<script>
function Alert()
{
this.strMsg;
this.showMsg = showMsg;
function showMsg()
{
alert(strMsg);
}
}
</script>
<script>
var alertObj = new Alert();
alertObj.strMsg = "hehe";
document.onclick = alertObj.showMsg; 
</script>

#7


pcsEntry.js


function Vector(){
    this.data = new Array();
    this.add = Vector_add;
    this.remove = Vector_remove;
    this.elementAt = Vector_elementAt;
    this.setElementAt = Vector_setElementAt;
    this.insert = Vector_insert;
    this.contains = Vector_contains;
    this.length = Vector_length;
    this.toString = Vector_toString;
}

function Vector_add( item ){
    this.data[ this.data.length ] = item;
}

function Vector_remove( index ){
    var data = this.data;
    data[ index ] = null;
    var tmpdata = new Array();
    var newindex = 0;
    for( var i = 0; i < data.length; i++ ){
        if( data[ i ] != null ) {
            tmpdata[ newindex ] = data[ i ];
            newindex++;
        }
    }
    this.data = tmpdata;
}

function Vector_removeItem( item ){
    var data = this.data;
    var tmpdata = new Array();
    var newindex = 0;
    for( var i = 0; i < data.length; i++ ){
        if( data[ i ] != item ){
            tmpdata[ newindex ] = data[ i ];
        }
        newindex++;
    }
    this.data = tmpdata;
}

function Vector_elementAt( index ){
    return this.data[ index ];
}

function Vector_setElementAt( index, item ){
    this.data[ index ] = item;
}

function Vector_insert( index, item ){
    if( index == this.data.length ){
        this.add( item );
        return;
    }
    var data = this.data;
    var tmpdata = new Array();
    var newindex = 0;
    for( var i = 0; i < data.length; i++ ){
        if( i == index ){
            tmpdata[ i ] = item;
            newindex++;
        }
        tmpdata[ newindex ] = data[ i ];
        newindex++;
    }
    this.data = tmpdata;
}

function Vector_contains( item ){
    for( var i = 0; i < this.data.length; i++ ){
        if( this.data[i] == item ){
            return true;
        }
    }
    return false;
}

function Vector_length(){
    return this.data.length;
}

function Vector_toString(){
    var dataString = "[ ";
    var data = this.data;
    for( var i = 0; i < data.length; i++ ){
        dataString += data[i] + " ";
    }
    dataString += "]";
    return dataString;
}
///////////////////////////////////////////////////////////////////////////////
var Public_FjObject=null;
function fenjuEntry(fenjuName){
    this.fenjuName=fenjuName;
this.PcsList=new Vector();
this.PcsIDList=new Vector();
this.PcsxqIDList=new Vector();
this.addPcs=function (PcsName,PcsID,PcsxqID){
        this.PcsList.add(PcsName);
        this.PcsIDList.add(PcsID);
        this.PcsxqIDList.add(PcsxqID);
    }
this.PcsCount=function (){
        return this.PcsList.length();
    }
this.getPcsName=function (index){
    return this.PcsList.elementAt(index);
    }
this.getPcsID=function (index){
    return this.PcsIDList.elementAt(index);
    }
this.getPcsxqID=function(index){
return this.PcsxqIDList.elementAt(index);
}
this.Clone=function(){
        var temp=new fenjuEntry(this.fenjuName);
for (var i=0;i<this.PcsCount();i++ ){
            temp.addPcs(this.getPcsName(i),this.getPcsID(i),this.getPcsxqID(i));
        }
return temp;
}
}
function getFjObject(FjName){
Public_FjObject=null;
switch(FjName){
       case m_dc: {Public_FjObject=dc.Clone();break;}
   case m_xc: {Public_FjObject=xc.Clone();break;}
   case m_cw: {Public_FjObject=cw.Clone();break;}
   case m_xw: {Public_FjObject=xw.Clone();break;}
   case m_cy: {Public_FjObject=cy.Clone();break;}
   case m_ft: {Public_FjObject=ft.Clone();break;}
   case m_sjs: {Public_FjObject=sjs.Clone();break;}
   case m_hd: {Public_FjObject=hd.Clone();break;}
}
}

var m_dc='东城*';
var m_xc='西城*';
var m_cw='崇文*';
var m_xw='宣武*';
var m_cy='朝阳*';
var m_ft='丰台*';
var m_sjs='石景山*';
var m_hd='海淀*';
eval('var dc=new fenjuEntry("'+m_dc+'");')
eval('var xc=new fenjuEntry("'+m_xc+'");')
eval('var cw=new fenjuEntry("'+m_cw+'");')
eval('var xw=new fenjuEntry("'+m_xw+'");')
eval('var cy=new fenjuEntry("'+m_cy+'");')
eval('var ft=new fenjuEntry("'+m_ft+'");')
eval('var sjs=new fenjuEntry("'+m_sjs+'");')
eval('var hd=new fenjuEntry("'+m_hd+'");')
//东城*
dc.addPcs("建国门派出所","361","45");
dc.addPcs("朝阳门派出所","361","45");
dc.addPcs("东四派出所","361","45");
dc.addPcs("景山派出所","361","45");
dc.addPcs("北新桥派出所","361","45");
dc.addPcs("东直门派出所","361","45");
dc.addPcs("和平里派出所","361","45");
dc.addPcs("交道口派出所","361","45");
dc.addPcs("安定门派出所","361","45");
dc.addPcs("东华门派出所","361","45");
dc.addPcs("东交民巷派出所","361","45");
dc.addPcs("北京站派出所","361","45");
dc.addPcs("安外大街派出所","361","45");
dc.addPcs("王府井大街派出所","361","45");
dc.addPcs("隆福寺街派出所","361","45");
dc.addPcs("东方广场派出所","361","45");

//西城*
xc.addPcs("西长安街派出所","361","45");
xc.addPcs("二龙路派出所","361","45");
xc.addPcs("丰盛派出所","361","45");
xc.addPcs("厂桥派出所","361","45");
xc.addPcs("福绥境派出所","361","45");
xc.addPcs("月坛派出所","361","45");
xc.addPcs("新街口派出所","361","45");
xc.addPcs("展览路派出所","361","45");
xc.addPcs("阜外派出所","361","45");
xc.addPcs("德外派出所","361","45");
xc.addPcs("西单大街派出所","361","45");
xc.addPcs("北海公园派出所","361","45");
xc.addPcs("动物园派出所","361","45");
xc.addPcs("府右街派出所","361","45");
xc.addPcs( "地安门派出所","361","45");
xc.addPcs("恭王府派出所","361","45");

//testUnit
function pf(msg){
document.writeln(msg+'<br>');
}
for (var i=0;i<dc.PcsCount();i++ ){
    pf(dc.fenjuName+' '+i+'、'+dc.getPcsName(i)+' '+dc.getPcsID(i)+' '+dc.getPcsxqID(i));
}
pf('---------------------------------------------------------------');

pf('<div style="color:red">克隆后</div>');
var obj=dc.Clone();
for (var i=0;i<obj.PcsCount();i++ ){
    pf(obj.fenjuName+' '+i+'、'+obj.getPcsName(i)+' '+obj.getPcsID(i)+' '+obj.getPcsxqID(i));
}
pf('---------------------------------------------------------------');
getFjObject('西城*');
for (var i=0;i<Public_FjObject.PcsCount();i++ ){
    pf(Public_FjObject.fenjuName+' '+i+'、'+Public_FjObject.getPcsName(i)+' '+Public_FjObject.getPcsID(i)+' '+Public_FjObject.getPcsxqID(i));
}
pf('---------------------------------------------------------------');
getFjObject('东城*');
for (var i=0;i<Public_FjObject.PcsCount();i++ ){
   pf(Public_FjObject.fenjuName+' '+i+'、'+Public_FjObject.getPcsName(i)+' '+Public_FjObject.getPcsID(i)+' '+Public_FjObject.getPcsxqID(i));
}




#8


test.htm

<SCRIPT LANGUAGE="JavaScript" src="pcsEntry.js"></script>

#9


一下午写的个类。

里面包括 
java Vector类的实现
     Clone的实现

#1


http://blog.csdn.net/gztoby/archive/2004/08/13/74248.aspx

#2


http://www.google.com/search?hl=zh-CN&q=javascript+%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1&lr=

可以

#3


net_lover(孟子E章),是我偶像,呵呵

#4


function MyClass()
{
    this.Property1 = "";
    this.Property2 = "";

    this.Method1 = function()
    {
    }
}

#5


我写了一个类,但他说有错误:‘strMsg’未定义,为甚么?
<script>
function Alert(strMsg)
{
this.strMsg = strMsg;
this.showMsg = showMsg;
function showMsg()
{
alert(strMsg);
}
}
</script>
<script>
var alertObj = new Alert("hehehehe");
document.onclick = alertObj.showMsg; 
</script>

#6


发错了,是这个有错误,上面的那个没错
<script>
function Alert()
{
this.strMsg;
this.showMsg = showMsg;
function showMsg()
{
alert(strMsg);
}
}
</script>
<script>
var alertObj = new Alert();
alertObj.strMsg = "hehe";
document.onclick = alertObj.showMsg; 
</script>

#7


pcsEntry.js


function Vector(){
    this.data = new Array();
    this.add = Vector_add;
    this.remove = Vector_remove;
    this.elementAt = Vector_elementAt;
    this.setElementAt = Vector_setElementAt;
    this.insert = Vector_insert;
    this.contains = Vector_contains;
    this.length = Vector_length;
    this.toString = Vector_toString;
}

function Vector_add( item ){
    this.data[ this.data.length ] = item;
}

function Vector_remove( index ){
    var data = this.data;
    data[ index ] = null;
    var tmpdata = new Array();
    var newindex = 0;
    for( var i = 0; i < data.length; i++ ){
        if( data[ i ] != null ) {
            tmpdata[ newindex ] = data[ i ];
            newindex++;
        }
    }
    this.data = tmpdata;
}

function Vector_removeItem( item ){
    var data = this.data;
    var tmpdata = new Array();
    var newindex = 0;
    for( var i = 0; i < data.length; i++ ){
        if( data[ i ] != item ){
            tmpdata[ newindex ] = data[ i ];
        }
        newindex++;
    }
    this.data = tmpdata;
}

function Vector_elementAt( index ){
    return this.data[ index ];
}

function Vector_setElementAt( index, item ){
    this.data[ index ] = item;
}

function Vector_insert( index, item ){
    if( index == this.data.length ){
        this.add( item );
        return;
    }
    var data = this.data;
    var tmpdata = new Array();
    var newindex = 0;
    for( var i = 0; i < data.length; i++ ){
        if( i == index ){
            tmpdata[ i ] = item;
            newindex++;
        }
        tmpdata[ newindex ] = data[ i ];
        newindex++;
    }
    this.data = tmpdata;
}

function Vector_contains( item ){
    for( var i = 0; i < this.data.length; i++ ){
        if( this.data[i] == item ){
            return true;
        }
    }
    return false;
}

function Vector_length(){
    return this.data.length;
}

function Vector_toString(){
    var dataString = "[ ";
    var data = this.data;
    for( var i = 0; i < data.length; i++ ){
        dataString += data[i] + " ";
    }
    dataString += "]";
    return dataString;
}
///////////////////////////////////////////////////////////////////////////////
var Public_FjObject=null;
function fenjuEntry(fenjuName){
    this.fenjuName=fenjuName;
this.PcsList=new Vector();
this.PcsIDList=new Vector();
this.PcsxqIDList=new Vector();
this.addPcs=function (PcsName,PcsID,PcsxqID){
        this.PcsList.add(PcsName);
        this.PcsIDList.add(PcsID);
        this.PcsxqIDList.add(PcsxqID);
    }
this.PcsCount=function (){
        return this.PcsList.length();
    }
this.getPcsName=function (index){
    return this.PcsList.elementAt(index);
    }
this.getPcsID=function (index){
    return this.PcsIDList.elementAt(index);
    }
this.getPcsxqID=function(index){
return this.PcsxqIDList.elementAt(index);
}
this.Clone=function(){
        var temp=new fenjuEntry(this.fenjuName);
for (var i=0;i<this.PcsCount();i++ ){
            temp.addPcs(this.getPcsName(i),this.getPcsID(i),this.getPcsxqID(i));
        }
return temp;
}
}
function getFjObject(FjName){
Public_FjObject=null;
switch(FjName){
       case m_dc: {Public_FjObject=dc.Clone();break;}
   case m_xc: {Public_FjObject=xc.Clone();break;}
   case m_cw: {Public_FjObject=cw.Clone();break;}
   case m_xw: {Public_FjObject=xw.Clone();break;}
   case m_cy: {Public_FjObject=cy.Clone();break;}
   case m_ft: {Public_FjObject=ft.Clone();break;}
   case m_sjs: {Public_FjObject=sjs.Clone();break;}
   case m_hd: {Public_FjObject=hd.Clone();break;}
}
}

var m_dc='东城*';
var m_xc='西城*';
var m_cw='崇文*';
var m_xw='宣武*';
var m_cy='朝阳*';
var m_ft='丰台*';
var m_sjs='石景山*';
var m_hd='海淀*';
eval('var dc=new fenjuEntry("'+m_dc+'");')
eval('var xc=new fenjuEntry("'+m_xc+'");')
eval('var cw=new fenjuEntry("'+m_cw+'");')
eval('var xw=new fenjuEntry("'+m_xw+'");')
eval('var cy=new fenjuEntry("'+m_cy+'");')
eval('var ft=new fenjuEntry("'+m_ft+'");')
eval('var sjs=new fenjuEntry("'+m_sjs+'");')
eval('var hd=new fenjuEntry("'+m_hd+'");')
//东城*
dc.addPcs("建国门派出所","361","45");
dc.addPcs("朝阳门派出所","361","45");
dc.addPcs("东四派出所","361","45");
dc.addPcs("景山派出所","361","45");
dc.addPcs("北新桥派出所","361","45");
dc.addPcs("东直门派出所","361","45");
dc.addPcs("和平里派出所","361","45");
dc.addPcs("交道口派出所","361","45");
dc.addPcs("安定门派出所","361","45");
dc.addPcs("东华门派出所","361","45");
dc.addPcs("东交民巷派出所","361","45");
dc.addPcs("北京站派出所","361","45");
dc.addPcs("安外大街派出所","361","45");
dc.addPcs("王府井大街派出所","361","45");
dc.addPcs("隆福寺街派出所","361","45");
dc.addPcs("东方广场派出所","361","45");

//西城*
xc.addPcs("西长安街派出所","361","45");
xc.addPcs("二龙路派出所","361","45");
xc.addPcs("丰盛派出所","361","45");
xc.addPcs("厂桥派出所","361","45");
xc.addPcs("福绥境派出所","361","45");
xc.addPcs("月坛派出所","361","45");
xc.addPcs("新街口派出所","361","45");
xc.addPcs("展览路派出所","361","45");
xc.addPcs("阜外派出所","361","45");
xc.addPcs("德外派出所","361","45");
xc.addPcs("西单大街派出所","361","45");
xc.addPcs("北海公园派出所","361","45");
xc.addPcs("动物园派出所","361","45");
xc.addPcs("府右街派出所","361","45");
xc.addPcs( "地安门派出所","361","45");
xc.addPcs("恭王府派出所","361","45");

//testUnit
function pf(msg){
document.writeln(msg+'<br>');
}
for (var i=0;i<dc.PcsCount();i++ ){
    pf(dc.fenjuName+' '+i+'、'+dc.getPcsName(i)+' '+dc.getPcsID(i)+' '+dc.getPcsxqID(i));
}
pf('---------------------------------------------------------------');

pf('<div style="color:red">克隆后</div>');
var obj=dc.Clone();
for (var i=0;i<obj.PcsCount();i++ ){
    pf(obj.fenjuName+' '+i+'、'+obj.getPcsName(i)+' '+obj.getPcsID(i)+' '+obj.getPcsxqID(i));
}
pf('---------------------------------------------------------------');
getFjObject('西城*');
for (var i=0;i<Public_FjObject.PcsCount();i++ ){
    pf(Public_FjObject.fenjuName+' '+i+'、'+Public_FjObject.getPcsName(i)+' '+Public_FjObject.getPcsID(i)+' '+Public_FjObject.getPcsxqID(i));
}
pf('---------------------------------------------------------------');
getFjObject('东城*');
for (var i=0;i<Public_FjObject.PcsCount();i++ ){
   pf(Public_FjObject.fenjuName+' '+i+'、'+Public_FjObject.getPcsName(i)+' '+Public_FjObject.getPcsID(i)+' '+Public_FjObject.getPcsxqID(i));
}




#8


test.htm

<SCRIPT LANGUAGE="JavaScript" src="pcsEntry.js"></script>

#9


一下午写的个类。

里面包括 
java Vector类的实现
     Clone的实现