当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript:14-面向过程和面向对象、对象与类、面向对象版tab栏切换

JavaScript:14-面向过程和面向对象、对象与类、面向对象版tab栏切换

2020年07月22日  | 移动技术网IT编程  | 我要评论

JavaScript高级第01天笔记

1.面向过程与面向对象

1.1面向过程

  • 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候再一个一个的依次调用就可以了。

1.2面向对象

  • 面向对象是把事务分解成为一个个对象,然后由对象之间分工与合作。

1.3面向过程与面向对象对比

面向过程 面向对象
优点 性能比面向对象高,适合跟硬件联系很紧密的东西,例如单片机就采用的面向过程编程。 易维护、易复用、易扩展,由于面向对象有封装、继承、多态性的特性,可以设计出低耦合的系统,使系统 更加灵活、更加易于维护
缺点 不易维护、不易复用、不易扩展 性能比面向过程低

2.对象与类

2.1对象

对象是由属性和方法组成的:是一个无序键值对的集合,指的是一个具体的事物

  • 属性:事物的特征,在对象中用属性来表示(常用名词)
  • 方法:事物的行为,在对象中用方法来表示(常用动词)
<script>
    // 1. 创建类 class  创建一个 明星类
    class Star {
        constructor(uname, age) {
            this.uname = uname;
            this.age = age;
        }
    }

    // 2. 利用类创建对象 new
    var ldh = new Star('刘德华', 18);
    var zxy = new Star('张学友', 20);
    console.log(ldh);
    console.log(zxy);
    //(1) 通过class 关键字创建类, 类名我们还是习惯性定义首字母大写
    //(2) 类里面有个constructor 函数,可以接受传递过来的参数,同时返回实例对象
    //(3) constructor 函数 只要 new 生成实例时,就会自动调用这个函数, 如果我们不写这个函数,类也会自动生成这个函数
    //(4) 生成实例 new 不能省略
    //(5) 最后注意语法规范, 创建类 类名后面不要加小括号,生成实例 类名后面加小括号, 构造函数不需要加function
</script>

2.1.1创建对象

//以下代码是对对象的复习
//字面量创建对象
var ldh = {
    name: '刘德华',
    age: 18
}
console.log(ldh);

//构造函数创建对象
  function Star(name, age) {
    this.name = name;
    this.age = age;
 }
var ldh = new Star('刘德华', 18)//实例化对象
console.log(ldh);	

如上两行代码运行结果为:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l2I3rXfu-1595300861810)(images/img3.png)]

2.2类

  • 在 ES6 中新增加了类的概念,可以使用 class 关键字声明一个类,之后以这个类来实例化对象。类抽象了对象的公共部分,它泛指某一大类(class)对象特指某一个,通过类实例化一个具体的对象

2.2.1创建类

  1. 语法:
//步骤1 使用class关键字
class name {
  // class body
}     
//步骤2使用定义的类创建实例  注意new关键字
var xx = new name();     
  1. 示例
 // 1. 创建类 class  创建一个 明星类
 class Star {
   // 类的共有属性放到 constructor 里面
   constructor(name, age) {
   this.name = name;
   this.age = age;
   }
 }
   // 2. 利用类创建对象 new
   var ldh = new Star('刘德华', 18);
   console.log(ldh);

以上代码运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2azGJIMo-1595300861813)(images/img4.png)]

通过结果我们可以看出,运行结果和使用构造函数方式一样

2.2.2类创建添加属性和方法

<script>
    // 1. 创建类 class  创建一个 明星类
    class Star {
        // 类的共有属性放到 constructor 里面
        constructor(uname, age) {
            this.uname = uname;
            this.age = age;
        }
        sing(song) {
            // console.log('我唱歌');
            console.log(this.uname + song);

        }
    }

    // 2. 利用类创建对象 new
    var ldh = new Star('刘德华', 18);
    var zxy = new Star('张学友', 20);
    console.log(ldh);
    console.log(zxy);
    // (1) 我们类里面所有的函数不需要写function 
    //(2) 多个函数方法之间不需要添加逗号分隔
    ldh.sing('冰雨');
    zxy.sing('李香兰');
</script>

以上代码运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gyZ89owa-1595300861815)(images/img5.png)]

注意哟:

  1. 通过class 关键字创建类, 类名我们还是习惯性定义首字母大写
  2. 类里面有个constructor 函数,可以接受传递过来的参数,同时返回实例对象
  3. constructor 函数 只要 new 生成实例时,就会自动调用这个函数, 如果我们不写这个函数,类也会自动生成这个函数
  4. 多个函数方法之间不需要添加逗号分隔
  5. 生成实例 new 不能省略
  6. 语法规范, 创建类 类名后面不要加小括号,生成实例 类名后面加小括号, 构造函数不需要加function

2.2.3类的继承

  1. 语法
// 父类
class Father{   
} 

// 子类继承父类
class  Son  extends Father {  
}       
  1. 示例
class Father {
      constructor(surname) {
        this.surname= surname;
      }
      say() {
        console.log('你的姓是' + this.surname);
       }
}

class Son extends Father{  // 这样子类就继承了父类的属性和方法
}
var damao= new Son('刘');
damao.say();      //结果为 你的姓是刘

以上代码运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GL0QgLBN-1595300861817)(images/img6.png)]

<script>
    // 1. 类的继承
    // class Father {
    //     constructor() {

    //     }
    //     money() {
    //         console.log(100);

    //     }
    // }
    // class Son extends Father {

    // }
    // var son = new Son();
    // son.money();
    class Father {
        constructor(x, y) {
            this.x = x;
            this.y = y;
        }
        sum() {
            console.log(this.x + this.y);

        }
    }
    class Son extends Father {
        constructor(x, y) {
            super(x, y); //调用了父类中的构造函数
        }
    }
    var son = new Son(1, 2);
    var son1 = new Son(11, 22);
    son.sum();
    son1.sum();
</script>
  • 子类使用super关键字访问父类的方法

    //定义了父类
    class Father {
       constructor(x, y) {
       this.x = x;
       this.y = y;
       }
       sum() {
       console.log(this.x + this.y);
    	}
     }
    //子元素继承父类
        class Son extends Father {
       		 constructor(x, y) {
        		super(x, y); //使用super调用了父类中的构造函数
        	}
        }
        var son = new Son(1, 2);
        son.sum(); //结果为3
    
    <script>
        // super 关键字调用父类普通函数
        class Father {
            say() {
                return '我是爸爸';
            }
        }
        class Son extends Father {
            say() {
                // console.log('我是儿子');
                console.log(super.say() + '的儿子');
                // super.say() 就是调用父类中的普通函数 say()
            }
        }
        var son = new Son();
        son.say();
        // 继承中的属性或者方法查找原则: 就近原则
        // 1. 继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类的
        // 2. 继承中,如果子类里面没有,就去查找父类有没有这个方法,如果有,就执行父类的这个方法(就近原则)
    </script>
    

    注意:

    1. 继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类的

    2. 继承中,如果子类里面没有,就去查找父类有没有这个方法,如果有,就执行父类的这个方法(就近原则)

    3. 如果子类想要继承父类的方法,同时在自己内部扩展自己的方法,利用super 调用父类的构造函数,super 必须在子类this之前调用

       // 父类有加法方法
       class Father {
         constructor(x, y) {
         this.x = x;
         this.y = y;
         }
         sum() {
         console.log(this.x + this.y);
         }
       }
       // 子类继承父类加法方法 同时 扩展减法方法
       class Son extends Father {
         constructor(x, y) {
         // 利用super 调用父类的构造函数 super 必须在子类this之前调用,放到this之后会报错
         super(x, y);
         this.x = x;
         this.y = y;
      
        }
        subtract() {
        console.log(this.x - this.y);
        }
      }
      var son = new Son(5, 3);
      son.subtract(); //2
      son.sum();//8
      

      以上代码运行结果为:

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Et4Qm1z0-1595300861819)(images/img7.png)]

    4. 时刻注意this的指向问题,类里面的共有的属性和方法一定要加this使用.

      1. constructor中的this指向的是new出来的实例对象
      2. 自定义的方法,一般也指向的new出来的实例对象
      3. 绑定事件之后this指向的就是触发事件的事件源
    5. 在 ES6 中类没有变量提升,所以必须先定义类,才能通过类实例化对象[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3FlROLe6-1595300861823)(images/img2.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hs8x8URr-1595300861825)(images/img1.png)]

<body>
    <button>点击</button>
    <script>
        var that;
        var _that;
        class Star {
            constructor(uname, age) {
                // constructor 里面的this 指向的是 创建的实例对象
                that = this;
                console.log(this);

                this.uname = uname;
                this.age = age;
                // this.sing();
                this.btn = document.querySelector('button');
                this.btn.onclick = this.sing;
                //这里的sing不要加括号
                //加了括号会立即执行sing这个函数
            }
            sing() {
                // 这个sing方法里面的this 指向的是 btn 这个按钮,因为这个按钮调用了这个函数
                console.log(this);

                console.log(that.uname); // that里面存储的是constructor里面的this
            }
            dance() {
                // 这个dance里面的this 指向的是实例对象 ldh 因为ldh 调用了这个函数
                _that = this;
                console.log(this);

            }
        }

        var ldh = new Star('刘德华');
        console.log(that === ldh);
        ldh.dance();
        console.log(_that === ldh);

        // 1. 在 ES6 中类没有变量提升,所以必须先定义类,才能通过类实例化对象

        // 2. 类里面的共有的属性和方法一定要加this使用.
    </script>
</body>

3.面向对象版tab 栏切换

3.1功能需求

  1. 点击 tab栏,可以切换效果.
  2. 点击 + 号, 可以添加 tab 项和内容项.
  3. 点击 x 号, 可以删除当前的tab项和内容项.
  4. 双击tab项文字或者内容项文字可以修改里面的文字内容

3.2案例准备

  1. 获取到标题元素
  2. 获取到内容元素
  3. 获取到删除的小按钮 x号
  4. 新建js文件,定义类,添加需要的属性方法(切换,删除,增加,修改)
  5. 时刻注意this的指向问题

3.3切换

  • 为获取到的标题绑定点击事件,展示对应的内容区域,存储对应的索引

     this.lis[i].index = i;
     this.lis[i].onclick = this.toggleTab;
    
  • 使用排他,实现只有一个元素的显示

     toggleTab() {
       //将所有的标题与内容类样式全部移除
         for (var i = 0; i < this.lis.length; i++) {
         this.lis[i].className = '';
         this.sections[i].className = '';
         }
       //为当前的标题添加激活样式
         this.className = 'liactive';
        //为当前的内容添加激活样式
         that.sections[this.index].className = 'conactive';
      }
    

3.4添加

  • 为添加按钮+ 绑定点击事件

     this.add.onclick = this.addTab;
    
  • 实现标题与内容的添加,做好排他处理

    addTab() {
        that.clearClass();
        // (1) 创建li元素和section元素 
        var random = Math.random();
        var li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi">				</span></li>';
        var section = '<section class="conactive">测试 ' + random + '</section>';
        // (2) 把这两个元素追加到对应的父元素里面
        that.ul.insertAdjacentHTML('beforeend', li);
        that.fsection.insertAdjacentHTML('beforeend', section);
        that.init();
        }
    

3.5删除

  • 为元素的删除按钮x绑定点击事件

     this.remove[i].onclick = this.removeTab;
    
  • 获取到点击的删除按钮的所在的父元素的所有,删除对应的标题与内容

     removeTab(e) {
         e.stopPropagation(); // 阻止冒泡 防止触发li 的切换点击事件
         var index = this.parentNode.index;
         console.log(index);
         // 根据索引号删除对应的li 和section   remove()方法可以直接删除指定的元素
         that.lis[index].remove();
         that.sections[index].remove();
         that.init();
         // 当我们删除的不是选中状态的li 的时候,原来的选中状态li保持不变
         if (document.querySelector('.liactive')) return;
         // 当我们删除了选中状态的这个li 的时候, 让它的前一个li 处于选定状态
         index--;
         // 手动调用我们的点击事件  不需要鼠标触发
         that.lis[index] && that.lis[index].click();
     }
    

3.6编辑

  • 为元素(标题与内容)绑定双击事件

     this.spans[i].ondblclick = this.editTab;
     this.sections[i].ondblclick = this.editTab;
    
  • 在双击事件处理文本选中状态,修改内部DOM节点,实现新旧value值的传递

    editTab() {
        var str = this.innerHTML;
        // 双击禁止选定文字
        window.getSelection ? window.getSelection().removeAllRanges() : 				    document.selection.empty();
        // alert(11);
          this.innerHTML = '<input type="text" />';
          var input = this.children[0];
          input.value = str;
          input.select(); // 文本框里面的文字处于选定状态
          // 当我们离开文本框就把文本框里面的值给span 
          input.onblur = function() {
          this.parentNode.innerHTML = this.value;
          };
          // 按下回车也可以把文本框里面的值给span
          input.onkeyup = function(e) {
          if (e.keyCode === 13) {
          // 手动调用表单失去焦点事件  不需要鼠标离开操作
          this.blur();
          }
        }
    }
    

3.7完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>面向对象 Tab</title>
    <link rel="stylesheet" href="./styles/tab.css">
    <link rel="stylesheet" href="./styles/style.css">
</head>

<body>

    <main>
        <h4>
            Js 面向对象 动态添加标签页
        </h4>
        <div class="tabsbox" id="tab">
            <!-- tab 标签 -->
            <nav class="fisrstnav">
                <ul>
                    <li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试2</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试3</span><span class="iconfont icon-guanbi"></span></li>
                </ul>
                <div class="tabadd">
                    <span>+</span>
                </div>
            </nav>

            <!-- tab 内容 -->
            <div class="tabscon">
                <section class="conactive">测试1</section>
                <section>测试2</section>
                <section>测试3</section>
            </div>
        </div>
    </main>

    <script src="js/tab.js"></script>
</body>

</html>
* {
    margin: 0;
    padding: 0;
}

ul li {
    list-style: none;
}

main {
    width: 960px;
    height: 500px;
    border-radius: 10px;
    margin: 50px auto;
}

main h4 {
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.tabsbox {
    width: 900px;
    margin: 0 auto;
    height: 400px;
    border: 1px solid lightsalmon;
    position: relative;
}

nav ul {
    overflow: hidden;
}

nav ul li {
    float: left;
    width: 100px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border-right: 1px solid #ccc;
    position: relative;
}

nav ul li.liactive {
    border-bottom: 2px solid #fff;
    z-index: 9;
}

#tab input {
    width: 80%;
    height: 60%;
}

nav ul li span:last-child {
    position: absolute;
    user-select: none;
    font-size: 12px;
    top: -18px;
    right: 0;
    display: inline-block;
    height: 20px;
}

.tabadd {
    position: absolute;
    /* width: 100px; */
    top: 0;
    right: 0;
}

.tabadd span {
    display: block;
    width: 20px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    border: 1px solid #ccc;
    float: right;
    margin: 10px;
    user-select: none;
}

.tabscon {
    width: 100%;
    height: 300px;
    position: absolute;
    padding: 30px;
    top: 50px;
    left: 0px;
    box-sizing: border-box;
    border-top: 1px solid #ccc;
}

.tabscon section,
.tabscon section.conactive {
    display: none;
    width: 100%;
    height: 100%;
}

.tabscon section.conactive {
    display: block;
}
@font-face {font-family: "iconfont";
  src: url('./iconfont/iconfont.eot?t=1553960438096'); /* IE9 */
  src: url('./iconfont/iconfont.eot?t=1553960438096#iefix') format('embedded-opentype'), /* IE6-IE8 */
  url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAK4AAsAAAAABmwAAAJrAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCCcAp4fwE2AiQDCAsGAAQgBYRtBzAbpQXIrrApw71oi3CCOyzEy8RvE4yIN8TD036/zp03qCYRjaJZNBFFS/gREoRGipQKofjuNrb+9XbTqrmXcqWzfTRDqFqWkhAJzYToaE6LQ7Q30CirRqSKMnj58DdIdrNAdhoTQJa5VGfLrtiAy+lPoAcZdUC57UljTR4TMAo4oL0xiqwYG8YueIHPCdTqYajty/t+bUpmrwvEnUK42lQhLMssVy1UNhzN4kmF6vSQVvMY/T5+HEU1SUXBbti7uBBrx++cgqJULp0GhAgBna5AgSkgE0eN6R1NwTitNt0yAI5VG7wr/8AljmoX7K+zq+tBF1Q8k9JTPWp1AjnJDgCzmM3bU0V31dsvV3M2eC6fHjaGfX/qS7U5Gr58vj6uD0bgxudyrV/OtHHyP+NZnpO1txbktjdY+3FB61+7nxeOzq8niGYnRwT3v3aZxeXf6rrNxl5//49WlEtZUUL1Pj3Bv1EO7MuG2namrCkbvcnApLUJtWpRhv2tzlRLx43kQ7WO2/FW6c5QqDZEZnYKFeosoVK1NdSa5E/XaVM1Ra7BhAEQmk0kjV5QaLbIzG5U6HRRqTkK1DqJtivrjMT1zJaNnIsihAiyQE3JdbszcW0Xiadzdl4d8UO0HSUGNDNXzl2hifYSO5pPjrorgdjUAAavoa5TKDZVUXD3kuuOOzh70fShvUiN2owtNsRxIREIIiATUCYpGO2aqXy/CxEeHcfuaKrLDiGbQ5kcEMsNIK8M5qCmR3mn8RFHOpcECBtlAAwWIZ2OAqV5kQoJXHvShORYBzrDZKhhb3uT8QPlrA3bmsKZV6i89DiTV2o1AAAA') format('woff2'),
  url('./iconfont/iconfont.woff?t=1553960438096') format('woff'),
  url('./iconfont/iconfont.ttf?t=1553960438096') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  url('./iconfont/iconfont.svg?t=1553960438096#iconfont') format('svg'); /* iOS 4.1- */
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-guanbi:before {
  content: "\e676";
}
var that;
class Tab {
    constructor(id) {
        // 获取元素
        that = this;
        this.main = document.querySelector(id);
        this.add = this.main.querySelector('.tabadd');
        // li的父元素
        this.ul = this.main.querySelector('.fisrstnav ul:first-child');
        // section 父元素
        this.fsection = this.main.querySelector('.tabscon');
        this.init();
    }
    init() {
            this.updateNode();
            // init 初始化操作让相关的元素绑定事件
            this.add.onclick = this.addTab;
            for (var i = 0; i < this.lis.length; i++) {
                this.lis[i].index = i;
                this.lis[i].onclick = this.toggleTab;
                this.remove[i].onclick = this.removeTab;
                this.spans[i].ondblclick = this.editTab;
                this.sections[i].ondblclick = this.editTab;

            }
        }
        // 因为我们动态添加元素 需要从新获取对应的元素
    updateNode() {
            this.lis = this.main.querySelectorAll('li');
            this.sections = this.main.querySelectorAll('section');
            this.remove = this.main.querySelectorAll('.icon-guanbi');
            this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child');
        }
        // 1. 切换功能
    toggleTab() {
            // console.log(this.index);
            that.clearClass();
            this.className = 'liactive';
            that.sections[this.index].className = 'conactive';
        }
        // 清除所有li 和section 的类
    clearClass() {
            for (var i = 0; i < this.lis.length; i++) {
                this.lis[i].className = '';
                this.sections[i].className = '';
            }
        }
        // 2. 添加功能
    addTab() {
            that.clearClass();
            // (1) 创建li元素和section元素 
            var random = Math.random();
            var li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi"></span></li>';
            var section = '<section class="conactive">测试 ' + random + '</section>';
            // (2) 把这两个元素追加到对应的父元素里面
            that.ul.insertAdjacentHTML('beforeend', li);
            that.fsection.insertAdjacentHTML('beforeend', section);
            that.init();
        }
        // 3. 删除功能
    removeTab(e) {
            e.stopPropagation(); // 阻止冒泡 防止触发li 的切换点击事件
            var index = this.parentNode.index;
            console.log(index);
            // 根据索引号删除对应的li 和section   remove()方法可以直接删除指定的元素
            that.lis[index].remove();
            that.sections[index].remove();
            that.init();
            // 当我们删除的不是选中状态的li 的时候,原来的选中状态li保持不变
            if (document.querySelector('.liactive')) return;
            // 当我们删除了选中状态的这个li 的时候, 让它的前一个li 处于选定状态
            index--;
            // 手动调用我们的点击事件  不需要鼠标触发
            that.lis[index] && that.lis[index].click();
        }
        // 4. 修改功能
    editTab() {
        var str = this.innerHTML;
        // 双击禁止选定文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        // alert(11);
        this.innerHTML = '<input type="text" />';
        var input = this.children[0];
        input.value = str;
        input.select(); // 文本框里面的文字处于选定状态
        // 当我们离开文本框就把文本框里面的值给span 
        input.onblur = function() {
            this.parentNode.innerHTML = this.value;
        };
        // 按下回车也可以把文本框里面的值给span
        input.onkeyup = function(e) {
            if (e.keyCode === 13) {
                // 手动调用表单失去焦点事件  不需要鼠标离开操作
                this.blur();
            }
        }
    }

}
new Tab('#tab');

每日作业-JavaScript高级第01天

1.子类继承父类的属性和方法

  • 题目描述

    一个父类中有money(钱) cars(车) house(房) company(公司) 属性,有管理(manage)的方法,子类要继承父类中的属性和方法

  • 训练目标

    能够理解继承的原理

  • 训练提示

    1. 创建父类添加属性和方法
    2. 子类通过extends关键字继承到父类的属性和方法
    3. 注意constructor和this的问题
<script>
    class Father {
        constructor(money, cars, house, company) {
            this.money = money;
            this.cars = cars;
            this.house = house;
            this.company = company;
        }
        manage() {
            console.log('管理公司');
        }
    }
    class Son extends Father { // 子类就继承了父类的属性和方法
        constructor(money, cars, house, company) {
            // 利用super 调用父类的构造函数
            super(money, cars, house, company);
        }
    }
    //创建实例
    var son = new Son('10000', '保时捷', '别墅', '网络公司');
    console.log(son);
    //调用方法
    son.manage();
</script>

2 - 完成面向对象版的tab栏

  • 题目描述

    把今天课上讲的面向对象版的tab栏案例写一遍

  • 训练目标

    能够理解面向对象编程的思想

本文地址:https://blog.csdn.net/Satan_Devil/article/details/107490083

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网