javascript深入浅出 OPP

概念

    定义:

        Object-oriented programming    面向对象程序设计

        对象是类的实例

        它将对象作为程序的基本单元

        将程序和数据封装在其中

        以提高软件的重用性,灵活性和扩展性

    特性:

    ​    ​继承

    ​    ​封装

    ​    ​多态


继承

    基于原型的继承:

    ​    ​prototype属性与原型

    ​    ​原型中的prototype指向父类

    改变prototype:

    ​    ​修改某项属性,前面受影响

    ​    ​重写prototype,前面不受影响

    实现继承的方式:

1
Student.prototype = new Person();
1
2
Student.prototype =
Object.create(Person.prototype);
1
2
3
4
5
6
7
if (!Object.create) {
    Object.create = function (prototype) {
        function F() {}
        F.prototype = prototype;
        return new F();
    };
}


模拟

    模拟重载:

    ​    ​根据参数类型和参数个数分别执行

    ​    ​通过arguments和typeof类型判断实现

    链式调用:

        核心:return this

    模块化:

    ​    ​函数立即执行返回给变量

1
2
3
4
5
6
7
8
9
var module = function () {
    var name = 'guo';
    function fun() {
    }
    return {
        name: name,
        fun: fun
    };
}();


 


posted @ 2017-03-02 22:07  前望  阅读(487)  评论(0编辑  收藏  举报