__CreateJSPath = function (js) { var scripts = document.getElementsByTagName("script"); var path = ""; for (var i = 0, l = scripts.length; i < l; i++) { var src = scripts[i].src; if (src.indexOf(js) != -1) { var ss = src.split(js); path = ss[0]; break; } } var href = location.href; href = href.split("#")[0]; href = href.split("?")[0]; var ss = href.split("/"); ss.length = ss.length - 1; href = ss.join("/"); if (path.indexOf("https:") == -1 && path.indexOf("http:") == -1 && path.indexOf("file:") == -1 && path.indexOf("\/") != 0) { path = href + "/" + path; } return path; } var bootPATH = __CreateJSPath("car.js"); var ctxPath = bootPATH.substring(0, bootPATH.lastIndexOf("/resources/")); /** * Car对象有唯一标志ID。<br> * 它还有当前位置经度、纬度、方向、速度等属性。 * @param carId * @returns */ function Car(carId, lngX, latY, name, state, drectn, speed) { this._id = carId; if(lngX && !isNaN(lngX)) { this._lngX = lngX; } else { throw new TypeError("汽车的经度 lngX 属性应该是个浮点数!"); } if(latY && !isNaN(latY)) { this._latY = latY; } else { throw new TypeError("汽车的纬度 latY 属性应该是个浮点数!"); } this._name = name; if(CarState.isCarState(state)) { this._state = state; } else { throw Error("传入的参数state不是汽车状态值!"); } if(CarDirection.isCarDirection(drectn)) { this._direction = drectn; } else { throw Error("传入的参数drectn不是汽车方向值!"); } if(speed && !isNaN(speed)) { this._speed = speed; } else { throw new TypeError("汽车的速度 speed 属性应该是个浮点数!"); } } Car.prototype = { constructor: Car //显式设置构造函数反向引用 }; /**************属性的Getter、Setter*******************/ Car.__defineGetter__("id", function() { return this._id; }); Car.__defineSetter__("id", function(newValue) { this._id = newValue; }); Car.__defineGetter__("lngX", function() { return this._lngX; }); Car.__defineSetter__("lngX", function(newValue) { if(!isNaN(newValue)) { this._lngX = newValue; } else { throw new TypeError("汽车的经度 lngX 属性应该是个浮点数!"); } }); Car.__defineGetter__("latY", function() { return this._latY; }); Car.__defineSetter__("latY", function(newValue) { if(!isNaN(newValue)) { this._latY = newValue; } else { throw new TypeError("汽车的纬度 latY 属性应该是个浮点数!"); } }); Car.__defineGetter__("name", function() { return this._name; }); Car.__defineSetter__("name", function(newValue) { this._name = newValue; }); Car.__defineGetter__("state", function() { return this._state; }); Car.__defineSetter__("state", function(newValue) { this._state = newValue; }); Car.__defineGetter__("direction", function() { return this._direction; }); Car.__defineSetter__("direction", function(newValue) { this._direction = newValue; }); Car.__defineGetter__("speed", function() { return this._speed; }); Car.__defineSetter__("speed", function(newValue) { if(!isNaN(newValue)) { this._speed = newValue; } else { throw new TypeError("汽车的速度 speed 属性应该是个浮点数!"); } }); /** * Car的image属性,根据state状态设定 */ Car.__defineGetter__("image", function() { if(this.state && CarState.isCarState(this.state) && this.direction && CarDirection.isCarDirection(this.direction)) { var imgUrl = getImgUrlByStateDirection(this.state, this.direction); return imgUrl; } else { return ""; } }); function getImgUrlByStateDirection(state, direction) { if(this.state && CarState.isCarState(this.state) && this.direction && CarDirection.isCarDirection(this.direction)) { var imgUrl = "/"; var color ; switch(state) { case CarState.OFFLINE: { imgUrl += "gray"; color = "gray"; break; } case CarState.MOVING: { imgUrl += "green"; color = "green"; break; } case CarState.STOPPING: { imgUrl += "red"; color = "red"; break; } } switch(direction) { case CarDirection.EAST : { imgUrl += ("car-" + color + "east"); break; } case CarDirection.NORTH : { imgUrl += ("car-" + color + "north"); break; } case CarDirection.NORTH_EAST : { imgUrl += ("car-" + color + "northeast"); break; } case CarDirection.NORTH_WEST : { imgUrl += ("car-" + color + "northwest"); break; } case CarDirection.SOUTH : { imgUrl += ("car-" + color + "south"); break; } case CarDirection.SOUTH_EAST : { imgUrl += ("car-" + color + "southeast"); break; } case CarDirection.SOUTH_WEST : { imgUrl += ("car-" + color + "southwest"); break; } case CarDirection.WEST : { imgUrl += ("car-" + color + "west"); break; } } } else { return ""; } } /** * Car状态枚举类 */ var CarState = { OFFLINE : 0, //离线 MOVING : 1, //行进中 STOPPING : 2, //停止 isCarState : function(param) { return 0 === param || 1 === param || 2 === param ; } }; /** * Car状态枚举类 */ var CarDirection = { NORTH : 1, NORTH_EAST : 2, EAST : 3, SOUTH_EAST : 4, SOUTH : 5, SOUTH_WEST : 6, WEST : 7, NORTH_WEST : 8, isCarDirection : function(param) { switch(param) { case 1:case 2: case 3: case 4: case 5: case 6: case 7: case 8: { return true; } default: { return false; } } } };
PS:开始部分待修改。