码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript中的面向对象

时间:2015-04-30 12:36:38      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

 

 学习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中的面向对象

标签:

原文地址:http://blog.csdn.net/luckyzhoustar/article/details/45392647

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!