MyApp.Base = function () { } var basePrototype = MyApp.Base["prototype"]; //对象克隆方法 basePrototype["Clone"] = function (myObj) { if (typeof (myObj) != 'object') return myObj; if (myObj == null) return myObj; var myNewObj = new Object(); for (var i in myObj) myNewObj[i] = this.Clone(myObj[i]); return myNewObj; } //页面导航方法 basePrototype["Navigate"]=function(url) { window.location.href = url; } //页面导航方法 basePrototype["NavigateTag"] = function (tag) { switch (tag) { case "myHome": this.Navigate(bootPATH + "app/html/myhome.html"); break; case "zone": this.Navigate(bootPATH + "app/html/header_sample.html"); break; } } basePrototype["SetSession"] = function (keyname, keyvalue) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = keyname + "=" + escape(keyvalue) + ";expires=" + exp.toGMTString(); } basePrototype["GetSession"] = function (keyname) { var aCookie = document.cookie.split("; "); var lastMatch = null; for (var i = 0; i < aCookie.length; i++) { var aCrumb = aCookie[i].split("="); if (keyname == aCrumb[0]) { lastMatch = aCrumb; } } if (lastMatch) { var v = lastMatch[1]; if (v === undefined) return v; return unescape(v); } return null; } basePrototype["ClearSession"] = function (keyname) { if (keyname == "") { window.localStorage.clear(); } else { document.cookie = null; } } basePrototype["Extend"] = function (subClass, superClass) { var F = function () { }; F.prototype = superClass.prototype; subClass.prototype = new F(); subClass.prototype.constructor = subClass; subClass.superclass = superClass.prototype; //加多了个属性指向父类本身以便调用父类函数 if (superClass.prototype.constructor == Object.prototype.constructor) { superClass.prototype.constructor = superClass; } }; //定义全局的Base类对象 var MyAppBase = new MyApp.Base();
用法如下:
//设置Session MyAppBase.SetSession("Username", "wahaha"); //如果设置的是一个实体类,则需要用JSON.stringify把实体转化为Json字符串保存 MyAppBase.SetSession("User", JSON.stringify(UserItem)); //获取Session MyAppBase.GetSession("Username"); //用JSON.parse把Json字符串转化为实体 JSON.parse(MyAppBase.GetSession("User")); //跳转页面 MyAppBase.Navigate(bootPATH + "app/html/cctvlist.html"); //继承类 MyAppBase.Extend(ChildClass , ParentClass);