1、获取某个月的天数

 function getDate (year, month) {
return new Date(year, month + 1, 0).getDate();
}

2、获取变量类型

 function getType (e) {
return Object.prototype.toString.apply(e);
}
 getType('aa');            //[object String]
getType(11); //[object Number]
getType(undefined); //[object Undefined]
getType([]); //[object Array]
getType({}); //[object Object]
getType(null); //[object Null]

简单处理下

function getType (e) {
return Object.prototype.toString.apply(e).replace(/\[object\s|\]/g, '');
}

jquery中方法

var class2type = {};

var toString = class2type.toString;

jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
function( i, name ) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
} );

通过class2type[ toString.call( obj ) ]判断变量obj类型。

3、去掉字符串前后的空格

jquery的trim()方法源码如下

// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/, // Use native String.trim function wherever possible
trim: trim ?
function( text ) {
return text == null ?
"" :
trim.call( text );
} : // Otherwise use our own trimming functionality
function( text ) {
return text == null ?
"" :
text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
},

修改下正则

function myTrim (str) {
var exp = /^\s+|\s+$/g;
return str == null ?
"" :
str.toString().replace( exp, "");
}

4、数组操作

var old = [],
new1,
new2;
new1 = old;
new2 = old.slice(0);
old.push(1);
console.log(old.length);
console.log(new1.length);
console.log(new2.length);
04-28 10:06