var __fns = {};function define(name,fn){ __fns[name] = fn;}function require(names,callback){ var fns = []; $(names).each(function(i,name){ fns.push(__fns[name]); }); callback.apply(window,fns);}
需要留意的是,在使用的时候,require当中的参数是函数,也就是说可以进行new操作的,而不是已经new过后的实例。
示例用法:
//预约时间 function ServiceOrderTime(orderTime){ this.userAddress = orderTime.userAddress;//小区街道 this.userAddressNo = orderTime.userAddressNo;//楼宇门派号 this.lngLatPoi = orderTime.lngLatPoi; this.scheduleTime = orderTime.scheduleTime; this.serviceIntention = orderTime.serviceIntention; this.orderDay = new OrderDay(); this.orderDay.fromTimelong(this.scheduleTime); this.orderTime = new OrderTime(); this.orderTime.fromTimelong(this.scheduleTime); } ServiceOrderTime.prototype.toJson = function(){ return { userAddress : this.userAddress, userAddressNo : this.userAddressNo, lngLatPoi : this.lngLatPoi, scheduleTime : this.scheduleTime, serviceIntention : this.serviceIntention, travelToNewCost : this.travelToNewCost }; } ServiceOrderTime.prototype.stringify = function(){ return this.lngLatPoi + '-' + this.scheduleTime + '-' + this.serviceIntention; } ServiceOrderTime.prototype.eq = function(other){ if ( other ) { return this.lngLatPoi == other.lngLatPoi && this.scheduleTime == other.scheduleTime && this.serviceIntention == other.serviceIntention; } return false; } ServiceOrderTime.prototype.travelCost = function(lnglatPoi,callback){ var me = this; getTimeCost(this.lngLatPoi,lnglatPoi,function(mins){ me.travelToNewCost = mins; callback(mins); }); } ServiceOrderTime.prototype.getLngLatPoi = function(callback){ var me = this; AMapUtils.getAddressPoi(me.userAddress, { successCB : function(location) { var theLnglatPoi = location.geocodes[0].location; me.lngLatPoi = theLnglatPoi.lng + ',' + theLnglatPoi.lat; callback(); }, errorCB : function(){ me.lngLatPoi = null;//查询出错 callback(); } }); } //到处到特定的作用域中 define('ServiceOrderTime',ServiceOrderTime);
require(["ServiceOrderTime","ServiceOrderTimes","Stylist","ServiceOrder"],function(ServiceOrderTime,ServiceOrderTimes,Stylist,ServiceOrder){ //prepare data var serviceOrder = new ServiceOrder(); 。。。});