国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Javascript中的面向对象

Javascript中的面向对象

来源:程序员人生   发布时间:2015-07-29 07:49:12 阅读次数:3023次

 

 学习JS这么长时间了,还是对其中面向对象的知识不太了解,最近抽出时间来总结1下,看1看JS中的对象世界。

 1.JS中的创建类

 在JS中创建类特别的容易,1般使用Function来声明

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


 2.JS中的类的属性

 创建了类,下面来看1下如何来自定义属性

 

<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.原型对象

  原型对象就好像是1个对象定义的备份,当代码援用属性时,如果它其实不存在于对象援用中,那末就会 自动在爱原型中查找这个属性

<span style="font-family:SimSun;font-size:18px;"> <!--原型对象--> <!--原型对象就好像是1个对象定义的备份,当代码援用属性时,如果它其实不存在于对象援用中,那末就会 自动在爱原型 中查找这个属性--> <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>



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生