• vue.js源码学习分享(八)


    /*  */
    
    
    var uid$1 = 0;
    
    /**
     * A dep is an observable that can have multiple
     * directives subscribing() to it.//一个dep 是一个可观察到的,dep能有多样的订阅指示
     */
    var Dep = function Dep () {
      this.id = uid$1++;
      this.subs = [];
    };
    
    Dep.prototype.addSub = function addSub (sub) {
      this.subs.push(sub);
    };
    
    Dep.prototype.removeSub = function removeSub (sub) {
      remove(this.subs, sub);
    };
    
    Dep.prototype.depend = function depend () {
      if (Dep.target) {
        Dep.target.addDep(this);
      }
    };
    
    Dep.prototype.notify = function notify () {
      // stablize the subscriber list first
      var subs = this.subs.slice();
      for (var i = 0, l = subs.length; i < l; i++) {
        subs[i].update();
      }
    };
    
    // the current target watcher being evaluated.//当前的目标观察者被评估
    // this is globally unique because there could be only one//这是全局唯一的因为这里只能只有一个
    // watcher being evaluated at any time.观察者随时被观察
    Dep.target = null;
    var targetStack = [];
    
    function pushTarget (_target) {
      if (Dep.target) { targetStack.push(Dep.target); }
      Dep.target = _target;
    }
    
    function popTarget () {
      Dep.target = targetStack.pop();
    }
    
    /*
     * not type checking this file because flow doesn't play well with//非类型检查这个文件,因为流不会很好的考虑动态的使用在数组原型上的方法
     * dynamically(动态) accessing(使用) methods on Array prototype
     */
    
    var arrayProto = Array.prototype;
    var arrayMethods = Object.create(arrayProto);
    [
    'push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse' ] .forEach(function (method) { // cache original method var original = arrayProto[method]; def(arrayMethods, method, function mutator () { var arguments$1 = arguments; // avoid leaking arguments://避开arguments的漏洞 // http://jsperf.com/closure-with-arguments var i = arguments.length; var args = new Array(i); while (i--) { args[i] = arguments$1[i]; } var result = original.apply(this, args); var ob = this.__ob__; var inserted; switch (method) { case 'push': inserted = args; break case 'unshift': inserted = args; break case 'splice': inserted = args.slice(2); break } if (inserted) { ob.observeArray(inserted); } // notify change ob.dep.notify(); return result }); });
  • 相关阅读:
    5.2 输出一张随机图片
    5.1 Request 获取请求数据的几种方法
    5.Servlet 对象(request-response)
    4.Servlet(动态web资源)
    复选框、单选按钮、下拉列表的定义
    选择屏幕输入值的控制
    屏幕元素创建的基本语法
    屏幕对象的F1/F4输入帮助功能
    函数alv下的颜色设置
    BDIA增强
  • 原文地址:https://www.cnblogs.com/liuhao-web/p/6672419.html
Copyright © 2020-2023  润新知