关于面向对象

  前言

8e5482b2ae06ac4d37beaad1a06256c2.png

 我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是面向对象的讲解

  环境配置

7dcfcba9cca1d88532cb22b0204b1bbe.png

npm init -y
yarn add vite -D

 修改page.json配置端口

77df009f44236dad61c6d08bb43ba414.png

{
  "name": "demo1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "vite --port 3002"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "vite": "^4.4.9"
  }
}

 简单案例

4e77544d6448cac5a876e05e22f10317.png

function Test(){
    var obj={
        a:1,
        b:2
    }
    return obj
}
console.log(Test())

 运行结果

c4f30b76bc80bdf4b86fcd290b4a7928.png

46f9c068b818513e83531663b58257fe.png

 面向对象案例

afb592dcc836509d769dbdf18ab7efd8.png

function Test(){
    this.a=1;
    this.b=2
    console.log(this)
    this.plus=function(){
        return this.a+this.b
    }
}
const test1=new Test()
const test2=new Test()
console.log(test1===test2)
console.log(test1.plus())

 运行结果

77374c1ca01ac32c894ef59097cc7c63.png

f9cfa4d8b38ad311739fc8caa33bc9c5.png

 面向对象案例

11daf762bf1b6e0bd5095655d939615d.png

function Test(a,b){
    this.a=a
    this.b=b
}
Test.prototype={
    plus:function(){
        console.log(this)
        return this.a+this.b
    }
}
const test1=new Test(1,2)
console.log(test1.plus())

 运行结果

ba80dacd527bbcad42be5aac0fa841ca.png

acde7d0db7dbda22578b3b5a87f8d2a0.png

 类组件

e7f42f881dafdbc21655dfb9a2c7cbfc.png

class Test{
    constructor(a,b){
        this.a=a
        this.b=b
    }
    plus=()=>{
        return this.a+this.b
    }
}
const test=new Test(1,2)
const test1=new Test(3,4)
console.log(test)
console.log(test1)
const {plus}=new Test(1,2)
const res=plus()
console.log(res)

 运行结果

89b7c50b0615b6353eb1e5fb49e0d7cc.png

7ac80e75ece6c7c6610183ca7806de28.png

继承

dc789ceb38fec9ae54640df4728a7dbe.png

class Test{
    constructor(a,b){
        this.a=a
        this.b=b
    }
    plus(){
        return this.a+this.b
    }
}
class Test1 extends Test{
    constructor(a,b){
       super(a,b)
    }
}




class Test2 extends Test{
    constructor(a,b){
       super(a,b)
    }
}
const test=new Test(1,2)
const test1=new Test(3,4)
console.log(test)
console.log(test1)




const res=new Test(1,2).plus()
const res1=new Test1(1,2).plus()
console.log(res)
console.log(res1)

 运行结果

9bf316c9c50e7012dadad4c7b5bbb8cd.png

52fc084603d4b536f43a730ecf4b3907.png

 轮播图案例

11b50af968c9e98ed4d3a9bf69f4df0e.png

  index.html

f405c079f87fce53bf201ec5d74b18ce.png

<!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>
    <div class="carousel" id="carousel-fade">
        <div class="img-stage">
            <div class="img-wrapper animate__animated">
                <img src="./geyao.jpg">
            </div>
            <div class="img-wrapper animate__animated">
                <img src="./fangfang.jpg">
            </div>
            <div class="img-wrapper animate__animated">
                <img src="./1.png">
            </div>
            <div class="img-wrapper animate__animated">
                <img src="./fangfang.jpg">
            </div>
        </div>
        <div class="indicator">
            <i class="dot"></i>
            <i class="dot"></i>
            <i class="dot"></i>
            <i class="dot"></i>
        </div>
    </div>
    <script type="module" src="./index5.js"></script>
</body>




</html>

 index5.js

c0f97e9278ef75b854a9a45c5f221920.png

import Fade from "./fade";
new Fade('#carousel-fade',{
    defaultIndex:0,
    duration:3000
})

 index.js

e046a73e762f6e06b00f458af832eb93.png

import './resets.css'
import './index.scss'
import 'animate.css';
export default class Fade{
    constructor(el,{
        defaultIndex,
        duration
    }){
        this.$el=document.querySelector(el)
        this.$imgWrapper=this.$el.querySelectorAll(".img-wrapper")
        this.$dotWrapper=this.$el.querySelector('.indicator')
        this.$dots=this.$dotWrapper.querySelectorAll('.dot')
        this.duration=duration
        this._index=defaultIndex
        this.init();
    
    }
    static t=null
    init(){
        this.show(true)
        this.bindEvent()
        this.play()
    }
    get currentIndex(){
        return this._index;
    }
    set currentIndex(newValue){
        // this._index=newValue
        this.update(()=>{
            this._index=newValue
        })
    }
    bindEvent(){
        this.$el.addEventListener('mouseenter',this.handlerMouseEnter.bind(this),false)
        this.$el.addEventListener('mouseenter',this.handlerMouseLeave.bind(this),false)
        this.$dotWrapper.addEventListener('click',this.handlerDotClick.bind(this),false)
    }
    handlerMouseEnter(){
        clearInterval(Fade.t)
    }
    handlerMouseLeave(){
        this.play()
    }
    handlerDotClick(e){
        console.log(e.target.className,"className is")
        e.target.className==='dot'&&(this.currentIndex=[].indexOf.call(this.$dots,e.target))
    }
    
    show(isInitial){
        if(isInitial){
            for(let i=0;i<this.$imgWrapper.length;i++){
                this.$imgWrapper[i].classList.add("animate__fadeOut")
            }
        }
        this.$imgWrapper[this.currentIndex].classList.remove('animate__fadeOut')
        this.$imgWrapper[this.currentIndex].classList.add('animate__fadeIn')
        this.$dots[this.currentIndex].classList.add('active')
    }
    hide(){
        this.$imgWrapper[this.currentIndex].classList.remove('animate__In')
        this.$dots[this.currentIndex].classList.remove('active')
        this.$imgWrapper[this.currentIndex].classList.add('animate__fadeOut')
    }
    update(setIndex){
        this.hide();
        setIndex();
        this.show()
    }
    play(){
        Fade.t=setInterval(()=>{
            this.currentIndex>=this.$imgWrapper.length-1?this.currentIndex=0:this.currentIndex++;
        },this.duration)
    }
}

 运行结果

f87539a2a9a108648019ccb0dea8de56.png

5b42338e76742bf3592808b40640569f.png

cd0272ba7b6eb2e793af2a3e28711c6a.png

outside_default.png

点击上方蓝字关注我们

outside_default.png

bccd104d4e6d2a5e8d11e607155581be.png

下方查看历史文章

f682bc86c17508a886ecc466895cd5ea.png

关于原型

关于this指向

关于回调

关于闭包(完)笔记

关于闭包(壹)

posted @ 2023-10-10 09:55  前端导师歌谣  阅读(12)  评论(0)    收藏  举报  来源