Javascript中的物件導向

來源:互聯網
上載者:User

標籤:

 

 學習JS這麼長時間了,還是對其中物件導向的知識不太瞭解,最近抽出時間來總結一下,看一看JS中的對象世界。

 1.JS中的建立類

 在JS中建立類特別的容易,一般使用Function來聲明

<span style="font-family:SimSun;font-size:18px;"><script>        //建立類        function People()        {        }        //判斷類的類型        var someone = new People();        alert(someone instanceof People);                    </script></span>


 2.JS中的類的屬性

 建立了類,下面來看一下如何來自訂屬性

 

<span style="font-family:SimSun;font-size:18px;"><script>        //類的屬性        function People(name, sex)        {            this.name = name;            this.sex = sex;        }        var susan = new People("Susan", "female");        alert(susan.name);        alert(susan.sex);    </script></span>

 3.JS中的類的方法

 

<span style="font-family:SimSun;font-size:18px;"><script>        //類的方法        function People(name, sex) {            this.name = name;            this.sex = sex;            this.changeName = function (newName) {                this.name = newName;            }        }        var someone = new People("Susan", "female");        alert(susan.name);        someone.changeName("Lily");        alert(someone.name);           </script></span>


 4.JS中類的公有屬性和私人屬性

 在類中通過this指標添加的屬性均為公有屬性,私人屬性均為var

 

<span style="font-family:SimSun;font-size:18px;">  <!--共有屬性和私人屬性-->    <!--在類中通過this指標添加的屬性均為公有屬性。公有屬性是可以被外部存取的屬性-->    <script>        function People(ndame, sex, deposit) {            this.name = name;            this.sex = sex;            var deposit = deposit;            this.changeName = function (newName) {                this.name = newName;            }            this.consume = function (money) {                if (deposit >= money) {                    deposit -= money;                } else {                    throw new Error("沒有足夠存款");                }            }        }        var susan = new People("susan", "female", 1000);        var name = susan.name;        var deposit = susan.deposit; //訪問不了,私人屬性        alert(deposit);        try {            susan.consume(500);        } catch (e) {            alert(e.message);        }        try {            susan.consume(1000);        } catch (e) {            alert(e.message);        }    </script></span>


 5.公有方法和私人方法

 屬性有公有和私人之分,方法也是。概念類似。

 

<span style="font-family:SimSun;font-size:18px;"> <!--公有方法和私用方法-->    <script>        function People(name, sex, deposit) {            this.name = name;            this.sex = sex;            var deposit = deposit; //私人屬性            this.thew = 1;  //公有屬性            this.changeName = function (newName) {                this.name = newName;            }            this.consume = function (money) {                this.consume = function (money) {                    if (deposit >= money) {                        deposit -= money;                    } else {                        throw new Error("沒有足夠存款");                    }                }            }            var _this = this;//儲存當前對象到變數,_this中供私人方法使用            var digest = function (food) {   //私人方法digest                _this.thew++;   //匿名函數內,this指標指向會發生變化,因此程式中使用_this儲存People對象引用            }            this.eat = function (food) {   //公有方法eat                digest(food);            }        }    </script></span>

 6.靜態屬性和方法

 物件導向中也有靜態屬性和方法,看看JS是如何做到的

 

<span style="font-family:SimSun;font-size:18px;">  <!--靜態屬性和靜態方法-->    <!--建構函式本身就是對象,直接將屬性和方法添加到這個對象中,則可以達到靜態屬性和靜態方法的效果-->    <script>        function People() { }        People.staticProperty = "靜態屬性";  //靜態屬性        People.staticMethod = function () { }  //靜態方法    </script></span>



 7.原型對象

  原型對象就好像是一個對象定義的備份,當代碼引用屬性時,如果它並不存在於對象引用中,那麼就會 自動在愛原型中尋找這個屬性

<span style="font-family:SimSun;font-size:18px;"> <!--原型對象-->    <!--原型對象就好像是一個對象定義的備份,當代碼引用屬性時,如果它並不存在於對象引用中,那麼就會 自動在愛原型    中尋找這個屬性-->    <script>        function People(name, sex, deposit) {            this.name = name;            this.sex = sex;            var deposit = deposit; //私人屬性                              this.consume = function (money) {                this.consume = function (money) {                    if (deposit >= money) {                        deposit -= money;                    } else {                        throw new Error("沒有足夠存款");                    }                }            }            var _this = this;//儲存當前對象到變數,_this中供私人方法使用            var digest = function (food) {   //私人方法digest                _this.thew++;   //匿名函數內,this指標指向會發生變化,因此程式中使用_this儲存People對象引用            }            this.eat = function (food) {   //公有方法eat                digest(food);            }        }        People.prototype = {            thew: 1,            changeName: function (newName) {                this.name = newName;            }        }    </script></span>




Javascript中的物件導向

聯繫我們

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