vue 2ソースブラウズ分析02

6405 ワード

1.コンポーネント初期化方法init
 Vue.prototype._init = function (options) {
    /* istanbul ignore if */
    if ("development" !== 'production' && config.performance && perf) {
      perf.mark('init');
    }

    var vm = this;
    //      ID
    vm._uid = uid++;
    //   vue  
    vm._isVue = true;
    if (options && options._isComponent) {
      //       $options
      initInternalComponent(vm, options);
    } else {
      //  options       data,components,filter,dereactor,      ,watch,props,method,computed           
    //props,direcotives
    // optionMergerStrategies
    // data options[data] obj
    //
    // components,filters,dereactors
    // watch
   // vm.$options = mergeOptions( // Vue.options options resolveConstructorOptions(vm.constructor), options || {}, vm ); } /* istanbul ignore else */ { initProxy(vm); } // _self vm._self = vm;
   // , $children push
   // $root = $parent $parent ,
   // $children = [],$refs={},_wather=null,_inacitve=null,_directInactive=null,_isMounted=null,_isDestroyed=null,isBeingDestroyed=null
initLifecycle(vm);
   //_event = {},_hasHookEvent = false
   //vm._events _events, _parentListeners vm._events
    initEvents(vm);
   
    initRender(vm);   //  vm._c  vm.createElement       
    callHook(vm, 'beforeCreate');  
    initInjections(vm); // resolve injections before data/props
    initState(vm);  
    initProvide(vm); // resolve provide after data/props
    callHook(vm, 'created');  //    created  

    /* istanbul ignore if */
    if ("development" !== 'production' && config.performance && perf) {
      vm._name = formatComponentName(vm, false);
      perf.mark('init end');
      perf.measure(((vm._name) + " init"), 'init', 'init end');
    }
    console.log(vm)
    if (vm.$options.el) {  //   el       el     
      vm.$mount(vm.$options.el);   
    }
  };
  
initState (vm)

function initState (vm) {
  vm._watchers = [];
  var opts = vm.$options;
  // vm._props , , if (opts.props) { initProps(vm, opts.props); } // vm if (opts.methods) { initMethods(vm, opts.methods); } if (opts.data) {
  // data initData(vm); } else { // Observer observe(vm._data = {}, true /* asRootData */); } // Compuetd , vm._computedWatchers ,get if (opts.computed) { initComputed(vm, opts.computed); }
  // watch watcher if (opts.watch) { initWatch(vm, opts.watch); } }
  
 
//   
/*
, , ,

run
get
evalute
updata
*/ var Watcher = function Watcher (vm, expOrFn, cb, options) { this.vm = vm; vm._watchers.push(this); // options if (options) { this.deep = !!options.deep; this.user = !!options.user; this.lazy = !!options.lazy; this.sync = !!options.sync; } else { this.deep = this.user = this.lazy = this.sync = false; } this.cb = cb; this.id = ++uid$2; // uid for batching this.active = true; this.dirty = this.lazy; // for lazy watchers this.deps = []; this.newDeps = []; this.depIds = new _Set(); this.newDepIds = new _Set(); this.expression = expOrFn.toString(); // parse expression for getter if (typeof expOrFn === 'function') { this.getter = expOrFn; } else { this.getter = parsePath(expOrFn); if (!this.getter) { this.getter = function () {}; "development" !== 'production' && warn( "Failed watching path: \"" + expOrFn + "\" " + 'Watcher only accepts simple dot-delimited paths. ' + 'For full control, use a function instead.', vm ); } } this.value = this.lazy ? undefined : this.get(); };
// value
/*

get : ,
addDep(dep) : , watcher dep subs , newDeps 。 wather dep , dep, subs wather
cleanupDep : newDeps wather deps , newDeps
run : wather , ck new,old
evaluate : this.value,dirty=false ,
depend : watcher
teardown : wather
update : (lazy) dirty = true, sysc , ,
*/
  
//        watcher             watcher   run  ,      updated      
/*
has id
queue watcher
*/ function queueWatcher (watcher) { var id = watcher.id; if (has[id] == null) { has[id] = true; if (!flushing) { queue.push(watcher); // wait } else { // if already flushing, splice the watcher based on its id // if already past its id, it will be run next immediately. var i = queue.length - 1; while (i >= 0 && queue[i].id > watcher.id) { i--; } queue.splice(Math.max(i, index) + 1, 0, watcher); } // queue the flush if (!waiting) { // waiting = true; nextTick(flushSchedulerQueue); // watcher run } } }
  
 
//         ,          exp        , watch           
Vue.prototype.$watch = function (expOrFn, cb, options) { // , unwatcher , expOrFn cb var vm = this; options = options || {}; options.user = true; var watcher = new Watcher(vm, expOrFn, cb, options); if (options.immediate) { cb.call(vm, watcher.value); // , } return function unwatchFn () { // watcher.teardown(); } };
  
 
http://www.cnblogs.com/jiebba/p/6575214.html 
http://www.cnblogs.com/jiebba私のブログ、見てみましょう!
もし間違いがあったら、伝言を残して修正してください.