zhehuaxuan / zhehuaxuan.github.io Goto Github PK
View Code? Open in Web Editor NEW个人高质量博客
Home Page: https://zhehuaxuan.github.io/
个人高质量博客
Home Page: https://zhehuaxuan.github.io/
理解MVVM的概念和原理,理解常用的双向绑定的方法,手动实现模型和视图的双向绑定。
MVVM的概念可以参考很多文章,简单的讲就是实现前端模型和数据的双向绑定。那么目前主流的实现方式是什么呢?
下面先来讲讲双向绑定在Vue的实现,Vue的双向绑定主要使用数据劫持和通知订阅
下面来看一个Object中的一个方法 defineProperties,Object.defineProperties(obj, props)简称对象定义属性,在JS中我们一般使用如下:
var person = {};
person.name = "xuan";
当然在IE8以上浏览器也可以这么用:
var person = {};
Object.defineProperties(person, {
"name": {
value: "xuan"
}
});
var person = {};
Object.defineProperties(person, {
"name": {
value: "xuan"
}
});
delete person.name;
看一下结果,结果没变,如下:
这个使用引出了defineProperties函数中的props的配置项,现在添加一个配置:
var person = {};
Object.defineProperties(person, {
"name": {
configurable: true, //可配置的
value: "xuan"
}
});
delete person.name;
var person = {};
Object.defineProperties(person, {
"name":
{
value: "xuan",
configurable: true
}
});
person.name = "xuan";
console.log(person)
person.name = "xue";
console.log(person);
结果改不了...,这里引出另一个配置项writable,如下:
var person = {};
Object.defineProperties(person, {
"name":
{
value: "xuan",
configurable: true,
writable:true
}
});
person.name = "xuan";
console.log(person)
person.name = "xue";
console.log(person);
var person = {};
Object.defineProperties(person, {
"name": {
value: "xuan",
configurable: true,
writable:true,
enumerable:true
}
});
person.name = "xuan";
for(let key in person){
console.log(key)
}
var person = {};
Object.defineProperties(person, {
"name":
{
configurable: true,
enumerable:true,
get:function(){
console.log("获取name属性:"+this.value);
return this.value
},
set:function(val){
console.log("设置name属性:"+val);
this.value = val;
}
}
});
person.name = "xuan";
console.log(person);
console.log(person.name)
person.name = "xue";
console.log(person);
console.log(person.name)
此处我们缺少了value和write的属性配置。value就是取值,writable就是可以配置,get和set属性用于配置方法,此处的作用也是用于获取和设置值。如上所示,在设置和获取值的时候我都加了一个console.log打印值,结果在我一旦修改和获取对象的值时,就会打印,这就是数据劫持。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.