第八章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>
相关推荐
源猿人2 小时前
企业级文件浏览系统的Vue实现:架构设计与最佳实践
前端·javascript·数据可视化
最后一个农民工2 小时前
vue3实现仿豆包模版式智能输入框
前端·vue.js
RoyLin2 小时前
TypeScript设计模式:迭代器模式
javascript·后端·node.js
小桥风满袖4 小时前
极简三分钟ES6 - ES9中for await of
前端·javascript
编程贝多芬4 小时前
Promise 的场景和最佳实践
前端·javascript
Asort4 小时前
JavaScript 从零开始(四):基础语法详解——从变量声明到数据类型的完全指南
前端·javascript
木木jio4 小时前
前端大文件分片上传 —— 基于 React 的工程化实现
前端·javascript
Lotzinfly4 小时前
12个TypeScript奇淫技巧你需要掌握😏😏😏
前端·javascript·面试
一个大苹果4 小时前
setTimeout延迟超过2^31立即执行?揭秘JavaScript定时器的隐藏边界
javascript
普郎特4 小时前
"不再迷惑!用'血缘关系'彻底搞懂JavaScript原型链机制"
前端·javascript