一、对象冒充
function student(name,age){ this.name = name; this.age = age; this.show = function(){ console.log("name:"+name,"age:"+age); } } function teacher(name,age){ this.teacher = student; this.teacher(name,age); delete this.teacher; } var ss = new teacher("xiaoming",22); ss.show(); var dd = new student("xiaolan",20); dd.show();
二、判断当天是星期几
function getMyDay(date){ var week; if(date.getDay()==0) week="周日"; if(date.getDay()==1) week="周一"; if(date.getDay()==2) week="周二"; if(date.getDay()==3) week="周三"; if(date.getDay()==4) week="周四"; if(date.getDay()==5) week="周五"; if(date.getDay()==6) week="周六"; return week; } var w1 = getMyDay(new Date("2015-7-12"));
四、判断当前日期为当月第几周
//判断当前日期为当月第几周 var getMonthWeek = function (a, b, c) { //a = d = 当前日期 //b = 6 - w = 当前周的还有几天过完(不算今天) //a + b 的和在除以7 就是当天是当前月份的第几周 var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate(); return Math.ceil((d + 6 - w) / 7); };
五、http请求带有多个参数的写法
CTSI.openWindow({width:500, height:430}, '编辑管理员', '/mts/user/ispLoginUserAdd.as?type=edit&id='+id+'&roleTypePwd='+roleType, null);
追加:如何获取url中“?”符号后的字符串
function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest; } var urlData = GetRequest();
六、判断浏览器的版本
var _uat = navigator.userAgent; _uat.indexOf("MSIE 9.0")>0 _uat.indexOf("Firefox")>0 if((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE6.0" || navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE7.0" || navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE8.0" || navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE9.0" )|| (_uat.indexOf("Firefox")<0) || (_uat.indexOf("Chrome")<0)) { alert("您的浏览器版本过低,请下载IE9以上版本"); }
七、js处理字符串常用的方法
功能:使用一个指定的分隔符把一个字符串分割存储到数组
例子:
str=”jpg|bmp|gif|ico|png”;
arr=theString.split(”|”);
//arr是一个包含字符值”jpg”、”bmp”、”gif”、”ico”和”png”的数组
函数:Join()
功能:字符串截取,比如想从"MinidxSearchEngine”中得到"Minidx”就要用到substring(0,6)
函数:indexOf()
定义和用法
substring 方法用于提取字符串中介于两个指定下标之间的字符。
语法
stringObject.substring(start,stop)
参数 描述
start 必需。一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。
stop 可选。一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的位置多 1。如果省略该参数,那么返回的子串会一直到字符串的结尾。
返回值
一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。
说明
substring 方法返回的子串包括 start 处的字符,但不包括 end 处的字符。
如果 start 与 end 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。
如果 start 比 end 大,那么该方法在提取子串之前会先交换这两个参数。
如果 start 或 end 为负数,那么它将被替换为 0。
2.substr 方法
定义和用法
substr 方法用于返回一个从指定位置开始的指定长度的子字符串。
语法
stringObject.substr(start [, length ])
参数 描述
start 必需。所需的子字符串的起始位置。字符串中的第一个字符的索引为 0。
length 可选。在返回的子字符串中应包括的字符个数。
说明
如果 length 为 0 或负数,将返回一个空字符串。

var hash = {}; activeJsonData = activeJsonData.reduce(function(item, next) { hash[next.key] ? '' : hash[next.key] = true && item.push(next); return item }, []);
十四、load和domcontentloaded事件区别