Code Monkey home page Code Monkey logo

说明

实现一个 MVVM 模式

实现逻辑

  • 1、将 options 参数绑定到 this 上
  • 2、beforeCreated 生命周期:如果有 beforeCreate 执行 beforeCreate 方法
  • 3、数据代理:将 data 和 methods 中的属性使用 Object.definePrototype 代理到 this 上,实现使用 this.key 访问到 data 和 methods 对象里的属性。
  • 4、数据劫持(核心):遍历 data 中的属性,使用 Object.definePrototype 给它们添加 get 和 set 方法,当获取时触发 get 方法如果是编译阶段的触发,就订阅这条信息(信息的来源在编译阶段存储到全局变量 window.target 中),如果是修改 data 中的数据触发 set 方法,并调用 Dep 类的发布方法。Dep 类中有一个数组用来存储依赖信息,有一个 on 方法向数组里面 push 来实现订阅,有一个 emit 方法实现遍历数组中的数据并触发里面对应的更新节点的方法。
  • 5、created 生命周期:如果有created执行 created 方法
  • 6、编译:获取所有 DOM 节点,将真实 DOM 转化为文档碎片存储在内存中提高性能。遍历内存中的 DOM 节点,如果 nodeType 是 1(元素) 或 3(文本节点)并且正则匹配到双大括号,则将更新节点的方法存储到全局变量 window.target 中,使用 node.textContent 方法实现双大括号内容的编译,然后清空全局变量;如果检测到 nodeType 是 1(元素节点) ,遍历元素节点的所有属性,如果属性名有@,就使用addEventListener绑定对应的事件,并把 methods 中对应的方法绑定到第二个参数上;如果是v-model则使用 addEventListener 绑定 input 事件,第二个参数方法中给对应的 data 属性赋值;其他的编译逻辑大致差不多
  • 7、mounted 生命周期:如果有 created 属性执行 created 方法
import Compile from "./compile.js";
import Observe from "./observe.js";
import Proxy from "./proxy.js";
export default class Jddk {
	constructor(options) {
		if (typeof options.data === "function") {
			this.$data = options.data();
		} else {
			this.$data = options.data;
		}
		this.$el = options.el;
		this.$methods = options.methods;
		this.$beforeCreate = options.beforeCreate;
		this.$created = options.created;
		this.$mounted = options.mounted;

		// beforeCreate生命周期:此时实例刚开始初始化,还没有将data中的数据进行代理,无法使用this.xx获取到data中的数据
		if (this.$beforeCreate) {
			this.$beforeCreate();
		}
		// 数据代理:将data的数据代理到this上
		new Proxy(this);

		// 数据劫持:检测data中的数据改变
		new Observe(this);

		// created生命周期:此时data中的数据已经全部绑定到了this上,但是HTML还没有开始编译
		if (this.$created) {
			this.$created();
		}

		// 编译器
		new Compile(this);

		// mounted生命周期:此时HTML已经编译完
		if (this.$mounted()) {
			this.$mounted();
		}
	}
}

第一次提交

实现 created,mounted 生命周期, 实现 v-model 双向数据绑定, 实现模板编译 实现 click,blur,focus 等事件

第二次提交

为了便于维护,将数据劫持、模版编译、数据监听的逻辑代码进行拆分

九段刀客's Projects

activiti icon activiti

Activiti is a light-weight workflow and Business Process Management (BPM) Platform targeted at business people, developers and system admins. Its core is a super-fast and rock-solid BPMN 2 process engine for Java. It's open-source and distributed under the Apache license. Activiti runs in any Java application, on a server, on a cluster or in the cl

administrative-divisions-of-china icon administrative-divisions-of-china

中华人民共和国行政区划:省级(省份直辖市自治区)、 地级(城市)、 县级(区县)、 乡级(乡镇街道)、 村级(村委会居委会) ,**省市区镇村二级三级四级五级联动地址数据。

browser-base icon browser-base

Modern and feature-rich web browser base based on Electron

build-scripts icon build-scripts

:octopus: 基于 Webpack 的插件化工程构建工具,支持快速建设一套开箱即用的工程方案。

cabloy icon cabloy

🚀 CabloyJS是一款自带工作流引擎的Node.js全栈框架,面向开发者的低代码开发平台。实现了真正意义的“一次开发,到处运行”的跨端跨平台理念。只需一套代码,即可同时实现B端中后台管理系统和C端前台应用。只需一套代码,即可同时跨端PC和Mobile,并且Mobile端是接近原生体验。A Node.js full-stack framework with workflow engine, based on koa + egg + vue + framework7

chatgpt-next-web icon chatgpt-next-web

A well-designed cross-platform ChatGPT UI (Web / PWA / Linux / Win / MacOS). 一键拥有你自己的跨平台 ChatGPT 应用。

datav icon datav

Vue数据可视化组件库(类似阿里DataV,大屏数据展示),提供SVG的边框及装饰、图表、水位图、飞线图等组件,简单易用,长期更新(React版已发布)

faq icon faq

前端常见问题笔记

gltf-pipeline icon gltf-pipeline

Content pipeline tools for optimizing glTF assets. :globe_with_meridians:

idatav icon idatav

大屏数据可视化 Big screen data visualization demo

jsnes-web icon jsnes-web

A browser UI for JSNES, a JavaScript NES emulator

lowcode-tools icon lowcode-tools

An enterprise-class low-code technology stack with scale-out design / 一套面向扩展设计的企业级低代码技术体系

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.