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

/*  */

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();
} /* */
05-11 19:25