close

按此前往javascript完整教學目錄

 

 

成員函式鍊:所謂成員函式鍊,是在一行程式碼中執行超過一個成員函式,寫法就像鎖鏈般一個連著一個,讓撰寫變得更為快速,更減少程式碼行數。製作函式鍊的範例如:

function create_student(name_,age_,score_,money_)

{

          var student_=new Object(

          {

                   name:name_,

                   age:age_,

                   score:score_,

                   money:money_,

                   re_name:function(s){this.name=s;return this;},  //成員函式回傳物件本身,使物件在執行成員函式後,在語法上仍是物件本身。

                   add_age:function(n){this.age+=n;return this;},

                   add_score:function(n){this.score+=n;return this;},

                   add_money:function(n){this.money+=n;return this;},

                   show_me:function(){

                             document.write("my name is "+this.name+","+this.age+" years ago."+"</br>");

                             document.write("I got "+this.score+" at this exam, then the teacher give me "+this.money+" $!!");

                             }

          });

          return student_;

}

 

var s1=create_student("jade",16,80,7000);

var s2=create_student("umei",12,60,10000);

s1.add_score(5).add_money(600).show_me();   //函式鍊在此!

//顯示:

my name is jade,16 years ago.
I got 85 at this exam, then the teacher give me 7600 $!!

 

 

 

上一篇:javascript 函式

下一篇:javascript 遞迴函式

arrow
arrow

    埃伯 發表在 痞客邦 留言(0) 人氣()