WebZero
1/14/2018 - 9:07 AM

模块示例

(function (window) {
  'use strict';
  var App = window.App || {};
  
  function Truck (truckId, db) {
    this.truckId = truckId;
    this.db = db;
  }
  
  Truck.prototype.createOrder = function (order) {
    this.db.add(order.emailAddress, order);
  }
  
  App.Truck = Truck;
  window.App = App;
})(window);

// 调用方法
var myTruck = new App.Truck('007', new App.DataStore());
myTruck.createOrder({ emailAddress: 'zl@email.com', coffee: 'decaf'});
(function (window) {
  'use strict';
  var App = window.App || {};
  
  function DataStore () {
    this.data = {}
  }
  
  // 添加
  DataStore.prototype.add = function (key, val) {
    this.data[key] = val;
  };
  
  // 获取指定数据
  DataStore.prototype.get = function (key) {
    return this.data[key];
  }
  
  // 获取全部数据
  DataStore.prototype.getAll = function () {
    return this.data;
  }
  
  // 删除指定数据
  DataStore.prototype.remove = function (key) {
    delete this.data[key];
  }
  
  App.DataStore = DataStore;
  window.App = App;
})(window);