JS面向对象

来源:互联网 发布:js修改css样式 生效 编辑:程序博客网 时间:2024/06/17 14:07

         关于原生JS的一些理解:

面向对象是什么?

      面向对象本身就是一种处理问题的思路,是对传统面向过程编程的一种补充和完善;

核心是类和对象的抽象封装和创建,通过创建的各种对象之间互相协作(互相调用函数)完成一个功能的完整流程;

通过面向对象的三大特征封装、继承、多态,来提高程序的健壮性、代码的复用性和功能的强大的扩展性。


核心:

                类和对象

特性:

                1.封装零散的数据,方便数据的统一管理和支配使用
                2.有利于提高代码的复用性、降低代码之间的耦合性、提升功能的扩展性;

 特征:

                封装、继承、多态

 使用:

  构造函数

                 JS模拟面向对象,出现的一个概念
                 专门被用来创建对象
                 和new关键字一起使用!
                  function Type(参数) {
                        this.属性 = 属性值;
                  }
                  var t = new Type();
                  名称:帕斯卡命名法
                  构造函数——原型对象——prototype
                  用来构建和挂载构造函数对应的对象公共的属性和行为函数的!
                  继承~

                     apply/call实现的假继承,这两个方法的作用和区别
                    apply(args, [])
                    call(args, ...obj)

那么什么是继承?
        在继承中,会出现两个类型【父类、子类,子类继承父类的属性和行为】

继承是用来做什么的:
        继承是通过子类继承父类实现父类中代码的重复利用!

继承的使用注意事项:
         尽量让一个父类有一个或者多个子类
         一个子类只继承一个父类

 通过Prototype原型对象和空对象结合使用来实现完整的继承!
                    Animal(){}
                                        F() {}
                        prototype ====  prototype
                                                      Cat() {}
                                         new F() === prototype

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Title</title>  
  6.     <script>  
  7.         /*var Animal = function(name, color) {  
  8.             this.name = name;  
  9.             this.color = color;  
  10.         }  
  11.         Animal.prototype.eat = function() {console.log("吃肉");}  
  12.   
  13.         var Cat = function(name, color, age, type) {  
  14.             // Animal.apply(this, [name, color]);  
  15.             Animal.call(this, name, color);  
  16.             this.age = age;  
  17.             this.type = type;  
  18.         }  
  19.         Cat.prototype.caches = function() {console.log("抓老鼠..");}  
  20.   
  21.         var cat = new Cat("tom", "黑色", 48, "猫科");  
  22.         console.log(cat);*/  
  23.   
  24.         /**  
  25.          * 2.通过prototype和空对象进行间接继承  
  26.          */  
  27.         var _extends = function(Child, Parent) {  
  28.             var F = function() {};  
  29.             F.prototype = Parent.prototype;  
  30.             Child.prototype = new F();  
  31.             Child.prototype.constructor = Child;  
  32.         }  
  33.   
  34.         var Animal = function(name,age) {  
  35.             this.name = name;  
  36.             this.age = age;  
  37.         }  
  38.         Animal.prototype.play = function() {console.log("play gaming!");}  
  39.   
  40.         var Cat = function(name, age, color) {  
  41.             this.color = color;  
  42.         }  
  43.   
  44.         _extends(Cat, Animal);  
  45.   
  46.         var cat = new Cat();  
  47.         console.log(cat);  
  48.     </script>  
  49. </head>  
  50. <body>  
  51.   
  52. </body>  
  53. </html


0 0