javascript中對象使用執行個體及簡介

來源:互聯網
上載者:User

 <script language="javascript">
 //Author : 東閣
 //Date: 2008-1-10
 
 //實現原型的定義
 //並聯絡定義類的屬性和類的方法以及執行個體屬性和執行個體方法
 function Circle(radius)
 {
  this.r=radius;  //定義了執行個體 半徑屬性
 }
 //定義了類的屬性
 Circle.PI=3.14;
 //定義類的原型的方法
 //通過把函數賦給建構函式的原型對象使它成為執行個體方法
 //javascript 1.1中,我們可以用函數直接量定義一個無需命名為Circle_area的函數.
 function Circle_area(){return this.r*this.r*Circle.PI;}
 Circle.prototype.area=Circle_area;
 
 //定義另外的一個函數,通過比較半徑的比較來得出那個圓的半徑大的作為較大的圓
 function Circle_max(a,b)
 {
  if(a.r>b.r) return a;
  else return b;
 }
 //由於該函數比較的是兩個Circle對象,所以將它看作對個別Circle對象進行操作的執行個體方法
 //是沒有意義的,但我們又不希望成為一個單獨的函數,所以我們把它賦給了一個建構函式使它成為類函數.
 Circle.max=Circle_max;
 //覆蓋了Object.prototype中的toString()方法,實現了C++和Java中類似的方法
 //不同的就是C# 中的那中重寫目的來實現的重載
 Circle.prototype.toString=function(){return "this Circle's radius is :"+this.r;}

 var c=new Circle(1.0);
 c.r=2.2;
 var a=c.area();
 var x=Math.exp(Circle.PI);
 var d=new Circle(1.2);
 var bigger=Circle.max(c,d);
 var strbigger=bigger.toString();
 document.write("c's radius is :"+c.r+"d's radius is :"+d.r+"the bigger is:"+strbigger);
 document.write("<br/>this Circle class's attibute PI is :"+Circle.PI);

 

 //定義一個常用的複數類型
 //Complex為複數類
 //複數就是一個實數和虛數的和,虛數是-1的平方根
 /*
 定義類的第一個是定義該類的建構函式。
 這個建構函式要初始化對象的所有執行個體屬性
 這些屬性是核心的“狀態變數”,是他們使類的各個執行個體互不相同的
 */
 function Complex(real,img)
 {
  this.x=real;
  this.y=img;
 }
/*
定義類的第二步是在建構函式的原型對象中定義它的執行個體方法(或其他屬性)
該對象定義的任何屬性都將被這個類的所有執行個體繼承
注意執行個體方法對this關鍵字的隱式操作
許多方法都不需要其它實際參數
*/
Complex.prototype.magitude=function(){
 return Math.sqrt(this.x*this.x+this.y*this.y);
};
//以上是一種函數直接量
//返回複數的相反數
Complex.prototype.reverse=function(){
 return new Complex(this.y,this.x);
}
//以一種有效方式將一個Complex對象轉換為一個字串
Complex.prototype.toString=function(){
 return "{"+this.x+"+"+this.y+"i}";
}
//返回一個複數的實數部分
//該函數是在將Complex對象作為原始值處理時調用的
Complex.prototype.valueOf=function(){
 return this.x;
}
/*
定義類的第三步是定義類方法
常量和其他必需的類屬性,作為建構函式自身的屬性(而不是建構函式的原型對象的屬性)
注意,類方法沒有使用關鍵字this,因為它們支隊他們的實際參數進行操作.
*/
//計算兩個複數的和並返回結果
Complex.add=function(a,b){
 return new Complex(a.x+b.x,a.y+b.y);
}
//計算兩個複數的差
Complex.subtract=function(a,b){
 return new Complex(a.x-b.x,a.y-b.y);
}
//兩個複數的乘積
Complex.mutiply=function(a,b){
 return new Complex(a.x*b.x-a.y*b.y,a.x*b.x+a.y*b.y);
}
//下面是一些有用的預定義複數
//他們被定義為類的屬性,這樣 它們就可以被用作常量了
Complex.zero=new Complex(0,0);
Complex.one=new Complex(1,0);
Complex.i=new Complex(0,1);
c1=new Complex(6.2,4.5);
c2=new Complex(5,3);
document.write("<br />c1.x="+c1.x+"c1.y= "+c1.y);
document.write("<br/>c2:   "+c2.toString());
document.write("<br />"+Complex.add(c2,c1).toString());
document.write("<br />"+c2.magitude());

//類的繼承
function MoreComplex(real,img)
{
 this.x=real;
 this.y=img;
}
/*
我們將它的原型對象作為Complex對象
這一意味著新類的執行個體將繼承MoreComplex.prototype
後者有Complex.prototype繼承而來
Complex.prototype又由Object.prototype繼承而來的
*/
MoreComplex.prototype=new Complex(0,0);
/*
這種方法有點缺陷就是,由於我們明確地把MoreComplex.prototype設成了我們所建立的一個對象
所以就覆蓋了javascript提供的原型對象,而且拋棄了給定的constructor屬性,該屬性引用的是建立
這個對象的建構函式,但是MoreComplex對象繼承了他的超類的constructor屬性,他自己並沒有這個
屬性。明確地設定這一屬性可以解決這一問題。
*/
MoreComplex.prototype.constructor=MoreComplex;
//注意:以上方法在javascript1.1中
//下面是給子類添加新方法或新特性
MoreComplex.prototype.swap=function(){
 var tmp=this.x;
 this.x=this.y;
 this.y=tmp;
}

var m=new MoreComplex(3.0,5.6);
document.write("<br />"+m.toString());
var n=m;
m.swap();
document.write("<br />"+m.toString());
n.swap();
document.write("<br />"+n.toString());
var i=n.magitude();
document.write("<br />"+i);

//關聯陣列的對象
function Test()
{
  document.write("x:"+n["x"]+"y:"+n["y"]);
 
}
Test();
//以上引用屬性的方法成功,在實際的使用中就可以靈活的訪問和添加類的屬性了
//以上表明了javascript除了可以像c++和Java一樣使用"."來取屬性,同時作為關聯陣列
//也表現出出色的效能

 </script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.