原生JS面向对象

来源:互联网 发布:2015年nba新秀体测数据 编辑:程序博客网 时间:2024/06/08 12:26

面向对象是什么?疑问

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

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

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

核心:

                类和对象

特性:

                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

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script>        /*var Animal = function(name, color) {            this.name = name;            this.color = color;        }        Animal.prototype.eat = function() {console.log("吃肉");}        var Cat = function(name, color, age, type) {            // Animal.apply(this, [name, color]);            Animal.call(this, name, color);            this.age = age;            this.type = type;        }        Cat.prototype.caches = function() {console.log("抓老鼠..");}        var cat = new Cat("tom", "黑色", 48, "猫科");        console.log(cat);*/        /**         * 2.通过prototype和空对象进行间接继承         */        var _extends = function(Child, Parent) {            var F = function() {};            F.prototype = Parent.prototype;            Child.prototype = new F();            Child.prototype.constructor = Child;        }        var Animal = function(name,age) {            this.name = name;            this.age = age;        }        Animal.prototype.play = function() {console.log("play gaming!");}        var Cat = function(name, age, color) {            this.color = color;        }        _extends(Cat, Animal);        var cat = new Cat();        console.log(cat);    </script></head><body></body></html>


0 0