第八章TypeScript class类

重点

implements 约束class类的

readonly 只能给属性用,只能读取

private 只能在内部使用

protected 给子类和内部去使用

public 哪里都能使用

super

父类的 prototype.constructor.call

static

可以给方法也可以给属性是一个静态方法 他的this指向的是 static

javascript 复制代码
// 1.class 的基本用法 继承 和 类型约束 implements
// 2.class 的修饰符 readonly private public protected
// 3. super 原理
// 4.静态方法 static
// 5. get set

// 重点
// implements 约束class类的
// readonly 只能给属性用,只能读取
// private 只能在内部使用
// protected 给子类和内部去使用
// public 哪里都能使用
// super
// 父类的 prototype.constructor.call
// static
// 可以给方法也可以给属性是一个静态方法
// 他的this指向的是 static


interface Options {
    el: string | HTMLElement
}

interface VueClas {
    options: Options

    init(): void
}

interface Vnode {
    tag: string // div section header
    text?: string//123
    children?: VNode[]
}

class Dom {
    // 创建节点的方法
    private createElement(el: string) {
        return document.createElement(el)
    }

//     填充文本的方法
    private setText(el: HTMLElement, text: string | null) {
        el.textContent = text
    }

//     渲染函数
    protected render(data: Vnode) {
        let root = this.createElement(data.tag)
        if (data.children && Array.isArray(data.children)) {
            data.children.forEach((item) => {
                let child = this.render(item)
                root.appendChild(child)
            })
        } else {
            this.setText(root, data.text)
        }

        return root
    }
}


class Vue extends Dom implements VueClas {
    readonly options: Options

    constructor(options: Options) {
        super()
        this.options = options
        this.init()
    }

    static xx() {

    }

    static version() {
        return '1.0.0'
    }

    init(): void {
        let data: Vnode = {
            tag: "div",
            children: [
                {
                    tag: "section",
                    text: "我是子节点1"
                },
                {
                    tag: "section",
                    text: "我是子节点2"
                },
                {
                    tag: "section",
                    text: "我是子节点3"
                }
            ]
        }
        let app = typeof this.options.el == 'string' ? document.querySelector(this.options.el) : this.options.el
        app.appendChild(this.render(data))
    }
}

new Vue({
    el: "#app"
})


// set get
class Ref {
    _value: any

    constructor(value: any) {
        this._value = value
    }

    // 这个地方的value和上面的value的不能重复
    get value() {
        return this._value + 'hm'
    }

    set value(newValue) {
        this._value = newValue + '鹤鸣'
    }
}


const ref = new Ref('鹤鸣')
console.log(ref.value) // 输出结果 鹤鸣hm
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script src="./index9.js"></script>
</body>
</html>
相关推荐
姚*鸿的博客12 分钟前
pinia在vue3中的使用
前端·javascript·vue.js
天下无贼!2 小时前
2024年最新版Vue3学习笔记
前端·vue.js·笔记·学习·vue
Jiaberrr2 小时前
JS实现树形结构数据中特定节点及其子节点显示属性设置的技巧(可用于树形节点过滤筛选)
前端·javascript·tree·树形·过滤筛选
我码玄黄2 小时前
THREE.js:网页上的3D世界构建者
开发语言·javascript·3d
爱喝水的小鼠3 小时前
Vue3(一) Vite创建Vue3工程,选项式API与组合式API;setup的使用;Vue中的响应式ref,reactive
前端·javascript·vue.js
小晗同学3 小时前
Vue 实现高级穿梭框 Transfer 封装
javascript·vue.js·elementui
WeiShuai3 小时前
vue-cli3使用DllPlugin优化webpack打包性能
前端·javascript
forwardMyLife3 小时前
element-plus的面包屑组件el-breadcrumb
javascript·vue.js·ecmascript
一介俗子3 小时前
TypeScript 中 extends 关键字
typescript
计算机学姐4 小时前
基于python+django+vue的影视推荐系统
开发语言·vue.js·后端·python·mysql·django·intellij-idea