JS面向对象高级特性


  对象的创建方法:

  对象的创建可以通过两种方式,第一种通过对象初始化的方法:

  1. var person={
  2. name:"xingoo",
  3. age:26,
  4. say:function(){
  5. console.log("say something");
  6. },
  7. action:function(){
  8. console.log("do something");
  9. }
  10. };
  11. console.log(person.name);
  12. console.log(person.age);
  13. person.say();
  14. person.action();


 第二种方式通过构造函数创建:


  1. function student(name,age){
  2. this.name = name;
  3. this.age = age;
  4. this.say = function(){
  5. console.log("say something");
  6. }
  7. this.action = function(){
  8. console.log("do something");
  9. }
  10. }
  11. var xingoo = new student("xingoo",27);
  12. console.log(xingoo.name);
  13. console.log(xingoo.age);
  14. xingoo.say();
  15. xingoo.action();

  对象的属性

  对象的属性分为对象属性、私有属性和类属性。

  对象属性需要创建对象后才能使用;

  私有属性在内部可以直接使用,在外部需要通过闭包才能使用。

  类属性可以通过对象名称直接使用。

  1. function func(){
  2. this.objPro1 = "对象属性";
  3. func.prototype.objPro2 = "对象属性";
  4. var privatePro = "私有属性";
  5. }
  6. func.classPro = "类属性";
  7. console.log(func.classPro);
  8. var f = new func();
  9. console.log(f.objPro1);
  10. console.log(f.objPro2);
  11. <!-- 私有属性可以通过闭包获取 -->

  对象的方法

  对象方法包括:对象方法,私有方法和类方法,使用类似前面的属性。

  1. function demoFunc1(){
  2. var privateFunc = function(){
  3. console.log("this is privateFunc");
  4. };
  5. privateFunc();
  6. this.objFunc1 = function(){
  7. console.log("this is objFunc1");
  8. };
  9. demoFunc1.prototype.objFunc2 = function(){
  10. console.log("this is objFunc2");
  11. };
  12. }
  13. demoFunc1.classFunc = function(){
  14. console.log("this is classFunc");
  15. };
  16. demoFunc1.classFunc();
  17. var f = new demoFunc1();
  18. f.objFunc1();
  19. f.objFunc2();


  继承、封装与多态

  JS要想实现继承,需要通过apply方法或者prototype实现。

  如果单纯的使用apply方法,子类的原型是子类;如果使用prototype,那么子类的原型也将继承父类。

  例如下面的代码:

  1. function Animal(name,age){
  2. this.name = name;
  3. this.age =age;
  4. this.say = function(){
  5. console.log("animal say something");
  6. }
  7. }
  8. function Cat(name,age){
  9. Animal.apply(this,[name,age]);
  10. }
  11. <!-- Cat.prototype = new Animal();-->
  12. var cat1 = new Cat("xingoo",3);
  13. console.log(cat1.name);
  14. console.log(cat1.age);
  15. cat1.say();


  上面代码中,cat的原型是cat;

  如果开启注释的部分,可以发现,cat类的原型也变成了Animal。

  子类的方法会覆盖父类的方法,即表现出多态性:

  1. function Pig(name,age){
  2. this.say = function(){
  3. console.log("i am pig");
  4. }
  5. }
  6. Pig.prototype = new Animal();
  7. function Dog(name,age){
  8. this.say = function(){
  9. console.log("i am dog");
  10. }
  11. }
  12. Dog.prototype = new Animal();
  13. function say(animal){
  14. if(animal instanceof Animal){
  15. animal.say();
  16. }
  17. }
  18. var dog = new Dog();
  19. var pig = new Pig();
  20. say(dog);
  21. say(pig);

  使用到的全部代码:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. </head>
  6. <body>
  7. <script type="text/javascript">
  8. <!-- 对象初始化器方式 -->
  9. var person={
  10. name:"xingoo",
  11. age:26,
  12. say:function(){
  13. console.log("say something");
  14. },
  15. action:function(){
  16. console.log("do something");
  17. }
  18. };
  19. console.log(person.name);
  20. console.log(person.age);
  21. person.say();
  22. person.action();
  23. <!-- 构造函数方式 -->
  24. function student(name,age){
  25. this.name = name;
  26. this.age = age;
  27. this.say = function(){
  28. console.log("say something");
  29. }
  30. this.action = function(){
  31. console.log("do something");
  32. }
  33. }
  34. var xingoo = new student("xingoo",27);
  35. console.log(xingoo.name);
  36. console.log(xingoo.age);
  37. xingoo.say();
  38. xingoo.action();
  39. <!-- 对象属性 私有属性,对象属性,类属性 -->
  40. function func(){
  41. this.objPro1 = "对象属性";
  42. func.prototype.objPro2 = "对象属性";
  43. var privatePro = "私有属性";
  44. }
  45. func.classPro = "类属性";
  46. console.log(func.classPro);
  47. var f = new func();
  48. console.log(f.objPro1);
  49. console.log(f.objPro2);
  50. <!-- 私有属性可以通过闭包获取 -->
  51. <!-- 私有方法,对象方法,类方法 -->
  52. function demoFunc1(){
  53. var privateFunc = function(){
  54. console.log("this is privateFunc");
  55. };
  56. privateFunc();
  57. this.objFunc1 = function(){
  58. console.log("this is objFunc1");
  59. };
  60. demoFunc1.prototype.objFunc2 = function(){
  61. console.log("this is objFunc2");
  62. };
  63. }
  64. demoFunc1.classFunc = function(){
  65. console.log("this is classFunc");
  66. };
  67. demoFunc1.classFunc();
  68. var f = new demoFunc1();
  69. f.objFunc1();
  70. f.objFunc2();
  71. <!-- 封装性,继承性,多态性 -->
  72. <!-- apply()实现属性和方法的集成,prototype实现原型的继承 -->
  73. function Animal(name,age){
  74. this.name = name;
  75. this.age =age;
  76. this.say = function(){
  77. console.log("animal say something");
  78. }
  79. }
  80. function Cat(name,age){
  81. Animal.apply(this,[name,age]);
  82. }
  83. <!-- Cat.prototype = new Animal();-->
  84. var cat1 = new Cat("xingoo",3);
  85. console.log(cat1.name);
  86. console.log(cat1.age);
  87. cat1.say();
  88. <!-- 继承 -->
  89. function Pig(name,age){
  90. this.say = function(){
  91. console.log("i am pig");
  92. }
  93. }
  94. Pig.prototype = new Animal();
  95. function Dog(name,age){
  96. this.say = function(){
  97. console.log("i am dog");
  98. }
  99. }
  100. Dog.prototype = new Animal();
  101. function say(animal){
  102. if(animal instanceof Animal){
  103. animal.say();
  104. }
  105. }
  106. var dog = new Dog();
  107. var pig = new Pig();
  108. say(dog);
  109. say(pig);
  110. </script>
  111. </body>
  112. </html>

  运行结果:


 

posted on 2017-03-22 09:29  signheart  阅读(724)  评论(0编辑  收藏  举报

导航