JavaScript學習-建構函式

來源:互聯網
上載者:User
   <%--JavaScript學習--%>
    <script type="text/javascript" language="JavaScript"> 
        //建立建構函式
        function Persion()
        {
            //建立建構函式成員
            this.name="scott";
            this.age="20";
            this.say=funSay;
        }
        //建構函式中的方法
        function funSay()
        {
            //調用建構函式中的成員 
            alert(this.name+"年齡為:"+this.age);
        }
        
        
        function PersionShow()

        {

        //執行個體建構函式

            var PerOne=new Persion();
            PerOne.say();
            var PerTwo=new Persion();
            PerTwo.say();
            /*
            說明:
            為一個對像執行個體增加的方法不會增加到同一對象所產生的其它執行個體上。
            所在執行個體對像在建立後會調用建構函式,對建構函式中增加的屬性和方法會被增加到所有建構函式的執行個體上。
            對象執行個體使用new關鍵字來建立,構造方法中返回任何結果,即不使用retrun語句。            
            this關鍵字代表某個成員方法執行時,引用該方法的當前執行個體。
            this關鍵字只在用作對象成員方法的函數中出現。
            */
        }
     </script>

            <%--測試使用的按鈕--%>
            <input id="Button1" type="button" runat="server" value="測試" onclick="PersionShow();" />

 

      <%--JavaScript學習--%>
    <script type="text/javascript" language="JavaScript"> 
        //建構函式(此為實建構函式)
        function Persion()
        {
        
        }
        //執行個體建構函式
        var Per=new Persion();
        //建構函式執行個體的成員1
        Per.age=18;
        //建構函式執行個體的成員2

        Per.name="test";

 

        //建構函式執行個體的中的函數

        function funSay()
        {
            //調用建構函式中的成員 
            alert(Per.name+"年齡為:"+Per.age);
            var xName="name";
            alert("另外一種訪問成員的方法結果:"+Per[xName]+"年齡為:"+Per["age"]);
            eval("alert('使用eval執行JavaScript後的結果:'+Per.name+'年齡為:'+Per['age'])");

            var vJs="alert('另一種eval執行JavaScript後的結果:"+Per.name+"年齡為:"+Per['age']+"')";
            eval(vJs);         
            
        }
        //構造函執行個體的數中的函數Say,Say實體為funSay函數
        Per.say=funSay;

        function PersionShow()
        {
           //調用建構函式執行個體的的函數
           Per.say();
            
        }
     </script>
     

            <%--測試使用的按鈕--%>
            <input id="Button1" type="button" runat="server" value="測試" onclick="PersionShow();" />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.