面向对象编程思想

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 面向对象的基本思想
        // 基本思路就是,创建一个对象,给这个对象,添加上属性和属性值,还有函数等方法
        // 之后通过操作这个对象,来完成需要的效果

        // 先通过一个函数的方法,来创建对象

        function createObj(){
            // 创建对象
            const obj = {};

            // 给对象添加属性和属性值
            obj.name = '张三';
            obj.age = 18;
            obj.addr = '北京';
            obj.sex = '男';

            // 给对象添加方法
            obj.funNameAge = function(){
                console.log(this.name , this.age);
            }
            obj.funNameAddr = function(){
                console.log(this.name , this.addr);
            }
            obj.funSexAge = function(){
                console.log(this.sex , this.age);
            }
            obj.funAll = function(){
                console.log(this.name, this.sex , this.age, this.addr);
            }

            // 返回这个创建好的对象
            return obj;
        }

        // 调用函数,函数创建对象,并且作为返回值
        // 变量中存储的就是函数创建的对象
        const obj = createObj();

        // 可以通过调用obj中存储的对方的方法,来实现功能
        obj.funAll();


    </script>
</body>
</html>

 

posted @ 2020-04-18 21:19  GLINLIND  Views(49)  Comments(0)    收藏  举报