Giter Club home page Giter Club logo

zhehuaxuan.github.io's People

Contributors

maseh87 avatar zhehuaxuan avatar

Watchers

 avatar

zhehuaxuan.github.io's Issues

MVVM原理

目的:

理解MVVM的概念和原理,理解常用的双向绑定的方法,手动实现模型和视图的双向绑定。

什么是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"
      }
 });

打印结果如下:
image
我现在加一行代码:

 var person = {};
 Object.defineProperties(person, {            
     "name": {
           value: "xuan"
      }
 });
delete person.name;

看一下结果,结果没变,如下:
image
这个使用引出了defineProperties函数中的props的配置项,现在添加一个配置:

 var person = {};
 Object.defineProperties(person, {            
     "name": {
           configurable: true, //可配置的
           value: "xuan"
      }
 });
delete person.name;

image
现在可以顺利的删除了,那我们再来修改一下这个属性,看看效果

    var person = {};
        Object.defineProperties(person, {
            "name":
            {
                value: "xuan",
                configurable: true
            }
        });
        person.name = "xuan";
        console.log(person)
        person.name = "xue";
        console.log(person);

image
结果改不了...,这里引出另一个配置项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);

结果如下:
image
在添加一个配置项enumerable:

 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)
 }

image

 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)

image
此处我们缺少了value和write的属性配置。value就是取值,writable就是可以配置,get和set属性用于配置方法,此处的作用也是用于获取和设置值。如上所示,在设置和获取值的时候我都加了一个console.log打印值,结果在我一旦修改和获取对象的值时,就会打印,这就是数据劫持

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.