<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body><div id="app"><input type="text" name="" v-model='num'><button v-click='addNum'>点击</button><h1 v-bind='num'></h1></div>
</body>
</html>
<script type="text/javascript">function MyVue(options) {this._init(options);}MyVue.prototype._init = function(options) {this.$options = options;this.$data = options.data;this.$el = document.querySelector(options.el);this.$methods = options.methods;this._binding = {}; /*** _binding保存着model与view的映射关系,也就是我们前面定义的Watcher的实例。* 当model改变时,我们会触发其中的指令类更新,保证view也能实时更新*/this._obverse(this.$data);this._compile(this.$el);};MyVue.prototype._obverse = function (obj) {var value;for(var i in obj){if(obj.hasOwnProperty(i)){value = obj[i];this._binding[i] = {_directives:[]}var binding = this._binding[i];if(typeof value === 'object'){this._obverse(value);}Object.defineProperty(this.$data,i,{enumerable:true,configurable:true,get:function () {console.log("获取值"+value);return value;},set:function (newVal) {console.log("设置值"+value);if(value !== newVal){value = newVal;binding._directives.forEach(function (item) {item.update();})}}})}}}MyVue.prototype._compile = function (root) {var _this = this;var nodes = root.children;for(var n = 0;n<nodes.length;n++){(function (node) {if(node.children.length !== 0){_this._compile(node);}if(node.hasAttribute('v-click')){var attrVal = node.getAttribute('v-click');node.addEventListener('click',(function () {return _this.$methods[attrVal].bind(_this.$data);})())}if(node.hasAttribute('v-bind')){var attrVal = node.getAttribute('v-bind');_this._binding[attrVal]._directives.push(new Watcher('text',node,_this,attrVal,'innerHTML'));}if(node.hasAttribute('v-model') && (node.tagName === 'INPUT' || node.tagName === 'TEXTAREA')){var attrVal = node.getAttribute('v-model');node.addEventListener('input',(function (key) {_this._binding[attrVal]._directives.push(new Watcher('input',node,_this,attrVal,'value'));return function () {_this.$data[attrVal] = node.value;}})(n));}})(nodes[n])}}function Watcher(name,el,vm,exp,attr) {this.name = name; //指令的名称this.el = el; //指令对应的节点this.vm = vm; //指令所属的vm实例this.exp = exp; // 指令对应的值this.attr = attr; //指令所需要的改变的属性this.update();}Watcher.prototype.update = function () {this.el[this.attr] = this.vm.$data[this.exp];}var app = new MyVue({el:'#app',data:{num:0},methods:{addNum:function () {console.log('num');this.num++;}}});
</script>复制代码

分享:你会写一个vue的双向数据绑定吗?相关推荐

  1. 面试题:你能写一个Vue的双向数据绑定吗?

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...

  2. 面试题:你能写一个 Vue 的双向数据绑定吗?

    作者:呆头呆脑丶 segmentfault.com/a/1190000014274840 在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理 ...

  3. 写一个Vue的双向数据绑定

    原文 https://segmentfault.com/a/1190000014274840 在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出 ...

  4. 你能写一个 Vue 的双向数据绑定吗?

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...

  5. 手写Vue 的双向数据绑定

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理. 本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希 ...

  6. Vue v-model双向数据绑定和一个简单的整数计算器

    一.v-model双向数据绑定 方法 v-bind - 单向数据绑定(从M到V) v-model - 双向数据绑定 例子 <input type="text" v-bind: ...

  7. 【Vue的双向数据绑定原理】

    Vue的双向数据绑定原理 先说面试答案: 1. 什么是setter.getter 2. 什么是Object.defineProperty() ? 先简单的实现一个js的双向数据绑定来熟悉一下`Obje ...

  8. 深入了解Vue的双向数据绑定

    前言 在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释 ...

  9. Vue 的双向数据绑定原理解析

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...

最新文章

  1. 从Netflix的Hystrix框架理解服务熔断和服务降级
  2. 收藏 | 一文总结70篇论文,帮你透彻理解神经网络的剪枝算法
  3. 服务器json文件怎么创建对象,JavaScript中对JSON对象的基本操作示例
  4. What is corresponding Cron expression to fire in every X seconds, where X 60? --转载
  5. pandas(三) -- DataFrame的基本操作
  6. python用电度数设计_无所不能的Python之配电设计自动化系统
  7. Linux 代码格式化工具 indent
  8. 论文浅尝 | 利用冻结语言模型的多模态少样本学习
  9. ProcessBuilder执行bash脚本
  10. svg标签的CSS3动画特效 - 经典特效2
  11. ls -l 显示年份
  12. j pocket_Wallabag:Pocket的开源替代品
  13. phpunit+selenium环境搭建
  14. 15年编程生涯,资深架构师总结的7条经验
  15. 华为又对这一领域下手了,网友:太难了……
  16. APP自动化-显示等待/强制等待/命令等待
  17. ipv6无网络访问权限可行解决方案
  18. 使用echarts图做Drink Flavors图
  19. 在短短几分钟内用冰柱构建超快速PHP服务器
  20. 信创-东方通和达梦适配

热门文章

  1. 问题 N: 算法设计与分析 输油管道
  2. Java (POI) 解析不同版本的word(doc、docx)
  3. js实现前端页面跳转后操作新页面
  4. 京东搜索--es和springboot加前端vue
  5. wx2tt 微信小程序转头条小程序工具
  6. excel vba计算平均数
  7. 基于javaweb的网上汽车销售系统(汽车商城管理系统)
  8. [modern c++] std::unique_ptr 的 get() 易错点
  9. python手势识别_10分钟用python实现手势识别
  10. 喜马拉雅的“边听边逛”新实验