vuejs源码摘抄

Posted 阿力瓦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vuejs源码摘抄相关的知识,希望对你有一定的参考价值。

订阅功能的部分实现代码如下:

/*  */


var uid = 0;

/**
 * A dep is an observable that can have multiple
 * directives subscribing to it.
 */
var Dep = function Dep () {
  this.id = uid++;
  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 () {
  // stabilize 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();
}

/*  */

 

以上是关于vuejs源码摘抄的主要内容,如果未能解决你的问题,请参考以下文章

[vuejs源码系列] auto detect CSS prefix

开发Vue插件四种方式

如何在 Vs Code 中更改默认自动选择的用户片段行为

《STL源码剖析》要点摘抄

Vuejs 2.0源码解析之渲染篇

如何阅读Vuejs源码,学习笔记