代码最重要的模仿,就是把形式背下来


模板

// view
let view = document.querySelector('.xxx')

// model
let model = {
  init: function() {},
  fetch: function() {},
  save: function() {}
}

// controller
let controller = {
  view: null,
  model: null,
  init: function(view, model) {
    this.view = view
    this.model = model
    this.model.init()
    this.bindEvents()
  },
  bindEvents: function() {}
}

// boot mvc
controller.init(view, model)

使用注意

xx.addEventListener('click', 函数) 中,函数的 this 指向要搞清楚

  1. 如果函数是成员函数,那么 this 指向 window,解决方法是 this.函数.bind(this)
  2. 如果函数是括号函数,那么 this 指向 controller
  3. 如果函数是匿名函数,那么 this 指向 xx (监听对象)