Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)原理變化的區(qū)別
defineProperty 定義對象的屬性,只不過屬性里的get和set實(shí)現(xiàn)了響應(yīng)式。
常用:
- value屬性值
- get
- set
- writeable 是否可寫
- enumrable 可遍歷
Vue從改變一個(gè)數(shù)據(jù)到發(fā)生改變的過程

Vue2.X數(shù)據(jù)響應(yīng)原理
創(chuàng)建頁面,實(shí)現(xiàn)延時(shí)2s修改對象的值。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LearnVue3.0</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
const vm = new vue();
setTimeout(function () {
console.log('change');
console.log(vm.$data);
vm.$data.a = 444;
}, 2000);
</script>
</body>
</html>
defineProperty 實(shí)現(xiàn):
function vue() {
this.$data = {
a: 1
};
this.el = document.getElementById('app');
this._html = "";
this.observe(this.$data);
this.render();
}
vue.prototype.observe = function (obj) {
let self = this;
let value;
for (let key in obj) {
value = obj[key];
if (typeof value === 'object') {
this.observe(value);
} else {
Object.defineProperty(this.$data, key, {
get: function () {
return value;
},
set: function (newvalue) {
value = newvalue;
self.render()
}
})
}
}
}
vue.prototype.render = function () {
this._html = "I am " + this.$data.a;
this.el.innerHTML = this._html;
}
在Chrome中console運(yùn)行,結(jié)果頁面顯示: I am 444
針對數(shù)組特性化處理:
let arraypro = Array.prototype;
// 為什么要create再次創(chuàng)建對象,create是深拷貝,不影響之前的arraypro
let arrayob = Object.create(arraypro);
// 定義哪些方法觸發(fā)更新
let arr = ["push", "pop", "shift"];
// arr里的方法,既能保持原有方法,又能觸發(fā)更新
// 裝飾者模式
arr.forEach(function (method, index) {
// 對自己的push方法重寫
arrayob[method] = function () {
let ret = arraypro[method].apply(this, arguments);
// self.render();
console.log('檢測到數(shù)組變化,觸發(fā)更新');
return ret;
}
});
在Chrome中console運(yùn)行示例:
let arr = []; arr.__proto__ = arrayob; arr.push(1);
結(jié)果顯示:
Vue3.0數(shù)據(jù)響應(yīng)原理
Vue3.0數(shù)據(jù)響應(yīng)原理
創(chuàng)建頁面,實(shí)現(xiàn)延時(shí)2s修改對象的值。代碼同上。
Proxy實(shí)現(xiàn):
function vue() {
this.$data = {
a: 1
};
this.el = document.getElementById('app');
this._html = "";
this.observe(this.$data);
this.render();
}
vue.prototype.observe = function (obj) {
let self = this;
this.$data = new Proxy(this.$data, {
get: function (target, key) {
return target[key];
},
set: function (target, key, newvalue) {
target[key] = newvalue;
self.render();
}
})
}
vue.prototype.render = function () {
this._html = "I am " + this.$data.a;
this.el.innerHTML = this._html;
}
在Chrome中console運(yùn)行,結(jié)果頁面顯示: I am 444
為什么改用Proxy
- defineProperty只能監(jiān)聽某個(gè)屬性,不能對全對象監(jiān)聽
- 可以省去for in循環(huán)提升效率
- 可以監(jiān)聽數(shù)組,不用再去單獨(dú)的對數(shù)組做特異性操作
Proxy還能做什么
校驗(yàn)類型
function createValidator(target, validator) {
return new Proxy(target, {
_validator: validator,
set(target, key, value, proxy) {
if(target.hasOwnProperty(key)) {
let validator = this._validator[key];
if(validator(value)) {
return Reflect.set(target, key, value, proxy);
} else {
throw Error('type error');
}
}
}
})
}
let personValidator = {
name(val) {
return typeof val === 'string';
},
age(val) {
return typeof val === 'number' && val > 18;
}
}
class person {
constructor(name, age) {
this.name = name;
this.age = age;
return createValidator(this, personValidator);
}
}
在Chrome中console運(yùn)行示例:
let tmp = new person('張三', 30);
結(jié)果顯示:

真正的私有變量
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 前端面試之vue2和vue3的區(qū)別有哪些
- vue2.x,vue3.x使用provide/inject注入的區(qū)別說明
- vue3的watch用法以及和vue2中watch的區(qū)別
- Vue2和Vue3在v-for遍歷時(shí)ref獲取dom節(jié)點(diǎn)的區(qū)別及說明
- 由淺入深講解vue2和vue3的區(qū)別
- vue2與vue3中生命周期執(zhí)行順序的區(qū)別說明
- vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說明
- Vue2與Vue3兄弟組件通訊bus的區(qū)別及用法
- Vue2.x與Vue3.x中路由鉤子的區(qū)別詳解
- 稍微學(xué)一下Vue的數(shù)據(jù)響應(yīng)式(Vue2及Vue3區(qū)別)
- 深入淺出分析vue2和vue3的區(qū)別
相關(guān)文章
vue+element-ui表格自定義列模版的實(shí)現(xiàn)
本文主要介紹了vue+element-ui表格自定義列模版的實(shí)現(xiàn),通過插槽完美解決了element-ui表格自定義列模版的問題,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
Element-ui?Layout布局(Row和Col組件)的實(shí)現(xiàn)
我們在實(shí)際開發(fā)中遇到一些布局的時(shí)候會(huì)用到Layout布局,本文就詳細(xì)的介紹了Element-ui?Layout布局(Row和Col組件)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12
詳解vue-cli項(xiàng)目中用json-sever搭建mock服務(wù)器
這篇文章主要介紹了詳解vue-cli項(xiàng)目中用json-sever搭建mock服務(wù)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Vue Vine實(shí)現(xiàn)一個(gè)文件中寫多個(gè)組件的方法(最近很火)
Vue Vine提供了全新Vue組件書寫方式,主要的賣點(diǎn)是可以在一個(gè)文件里面寫多個(gè)vue組件,Vue Vine是一個(gè)vite插件,vite解析每個(gè)模塊時(shí)都會(huì)觸發(fā)插件的transform鉤子函數(shù),本文介紹Vue Vine是如何實(shí)現(xiàn)一個(gè)文件中寫多個(gè)組件,感興趣的朋友一起看看吧2024-07-07
vue2 中使用 render 函數(shù)編寫組件的方式
vue提供了聲明式編寫UI的方式,即vue提供了對DOM進(jìn)行描述的方式,有兩種描述DOM的方式即模板和render 函數(shù),本文通過示例代碼介紹vue2 中使用 render 函數(shù)編寫組件的方式,感興趣的朋友跟隨小編一起看看吧2024-06-06

