首页 > 其他分享 >前端歌谣-第八课-关于面向对象

前端歌谣-第八课-关于面向对象

时间:2023-09-12 15:33:00浏览次数:36  
标签:console 歌谣 const 第八课 面向对象 plus Test new log

前言

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

环境配置

npm init -y
yarn add vite -D

修改page.json配置端口

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

简单案例

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

console.log(Test())

运行结果

前端歌谣-第八课-关于面向对象_html

面向对象案例

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())

运行结果

前端歌谣-第八课-关于面向对象_Test_02

面向对象案例

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())

运行结果

前端歌谣-第八课-关于面向对象_面向对象_03

类组件

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)

运行结果

前端歌谣-第八课-关于面向对象_Test_04

继承

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)

运行结果

前端歌谣-第八课-关于面向对象_html_05

轮播图案例

index.html

<!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

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

index.js

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)
    }
}

标签:console,歌谣,const,第八课,面向对象,plus,Test,new,log
From: https://blog.51cto.com/u_14476028/7445882

相关文章

  • 面向对象这么久了,还没找到对象?
    写代码的小伙伴们真幸福啊,想要对象了?没问题,new一个就好了。但是,new太多对象,对象也会生气的哦。你瞧,她来了从两段代码发现端倪我们来计算一个矩形的面积,看看这两段代码有什么区别呢?第一段:constheight=3;constwidth=5;letaaa=123;letbbb=456;letccc=789;//定......
  • Java是一种面向对象的编程语言
    Java是一种面向对象的编程语言,泰兰德幻化广泛应用于各种平台上。它的特点是可移植性强,安全性高,且具有很强的扩展性。Java语言采用了“一次编写,到处运行”的原则,这意味着可以在不同的操作系统和设备上运行相同的Java程序,无需对代码进行修改。Java语言有着丰富的类库和API,可以满足......
  • 前端歌谣-第六课-关于this指向
    前言我是歌谣最好的种树是十年前其次是现在今天继续给大家带来的是this的讲解环境配置npminit-yyarnaddvite-D修改page.json配置端口{"name":"demo1","version":"1.0.0","description":"","main":"index.js",......
  • JavaNote05-面向对象编程01
    0.面向对象内容的三条主线Java类及类的成员:(重点)属性、方法、构造器;(熟悉)代码块、内部类面向对象的特征:封装、继承、多态、(抽象)其他关键字的使用:this、super、package、import、static、final、interface、abstract等1.面向对象编程概述面向对象是软件开发中的一类编程风......
  • Java语言的特点,面向对象和面向过程的区别,八种基本数据类型的大小以及封装类
    1、Java语言有哪些特点1、简单易学、有丰富的类库2、面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高3、与平台无关性(JVM是Java跨平台使用的根本)4、可靠安全5、支持多线程2、面向对象和面向过程的区别面向过程是分析解决问题的步骤,然后用函数把这些步骤一步一步地实现,然后......
  • 前端歌谣-第六课-关于this指向
    前言我是歌谣最好的种树是十年前其次是现在今天继续给大家带来的是this的讲解环境配置npminit-yyarnaddvite-D修改page.json配置端口{"name":"demo1","version":"1.0.0","description":"","main":"index.js",......
  • JavaScript—面向对象、作用域
    C#:从类继承js:从对象继承什么叫继承?模板(类)原型继承(实体)有一个对象存在,构造函数设置原型为这个对象创建出来的对象就继承与这个对象(从对象那里继承)<scripttype="text/javascript">onload=function(){varPerson=function(){this.say=fu......
  • java基础-java面向对象-day08
    1.一个简单的类认识类成员变量类方法publicclassPerson{//类的成员变量intage;Stringname;doubleheight;doubleweight;publicvoideat(){System.out.println("吃饭");}publicvoidsleep(){System.out......
  • 面向对象—继承
    一.继承基本属性class 子类 extends父类{  }1.子类就会自动拥有父类定义的属性方法2.父类又叫超类,基类3.子类又叫派生类二.细节1.子类继承了所有的属性和方法,但是私有属性不能放在子类中直接访问,要通过公有的方法去访问。2.子类必须调用父类的构造器,完成父类的初始化3.当创......
  • 面向对象
    什么是面向对象面向对象&面向过程-面向过程思想-1步骤清晰简单,第一步做什么,第二部做什么-2面向过程适合处理一些简单的问题-面向过程思想-1物以类聚,分类的思维模式,思考问题首先会解决问题需要那些分类,然后对这些分类进行单独思考-......