Skip to content

Latest commit

 

History

History
196 lines (184 loc) · 4.6 KB

实用工具类.md

File metadata and controls

196 lines (184 loc) · 4.6 KB

检测类数组对象

// 判定o是否是一个类数组对象
// 字符串和函数有length属性,但是它们
// 可以用typeof检测将其排除。在客户端JavaScript中,DOM文本节点
// 也有length属性,需要用额外判断o.nodeType != 3将其排除
function isArrayLike (o){
    if (o &&                                    // o非null、undefined等
        typeof o === "object" &&                // o是对象
        isFinite(o.length) &&                   // o.length是有限数值
        o.length >= 0 &&                        // o.length是非负值
        o.length === Math.floor(o.length) &&    // o.length是整数
        o.length < 4294967296)                  // o.length < 2^32
    ){
        return true;                            // o是类数组对象
    }else{
        return false;                           // 否则它不是
    }
}

通过原型继承创建一个新对象

// 参考:《JavaScript权威指南第六版》p122
// inherit()返回一个继承自原型对象p的属性的新对象
// 这里使用ES5中的Object.create()函数(如果存在的话)
// 如果不存在Object.create(),则退化使用其他方法
function inherit(p){
    var t = typeof p;
    if(p == null || (t !== "object" && t !== "function")){
        throw TypeError();
    }
    if(Object.create){
        return Object.create(p);
    }
    
    function f(){}
    f.prototype = p;
    return new f();
}

获取某个对象的类属性

// 参考:《JavaScript权威指南第六版》p139
// Object.prototype.toString方法的返回如下格式的字符串:
// [object class]
// 并且class值的首字母大写的
function classof(o){
    if(o === null){
        return "Null";
    }else if(o === undefined){
        return "Undefined";
    }else{
        return Object.prototype.toString.call(o).slice(8, -1);
    }
}

生成随机数

生成某个范围内的随机数

function rand(min,max){
  return Math.random()*(max - min) + min;
}

生成某个范围内的随机数,如果只传递一个参数,那么将max赋值为min,min默认为0

function rand(min,max){
    if(!max){
        max = min;
        min = 0;
    }
    return Math.random()*(max - min) + min;
}

生成某个范围内的随机整数

function rand(min,max){
    return Math.floor(Math.random()*(max - min + 1) + min);
}

生成某个范围内的随机整数,如果只传递一个参数,那么将max赋值为min,min默认为0

function rand(min,max){
    if(!max){
        max = min;
        min = 0;
    }
    return Math.floor(Math.random()*(max - min + 1) + min);
}

去空格:

只去除左空格

function trimLeft(str){
    return str.replace(/^\s+/gm, "")
}

只去除右空格

function trimRight(str){
    return str.replace(/\s+$/gm, "")
}

去除左右空格

method1:

function trim(str){
    return trimLeft(trimRight(str))
}

method2:

function trim(str){
    return str.replace(/^\s+|\s+$/gm, "");
}

将字符串的首字母转为大写,其它字母都小写

method1

function capitalize(str){
    return str.slice(0,1).toUpperCase() + str.slice(1).toLowerCase()
}

method2

function capitalize (str){
    return str.charAt(0).toUpperCase() + str.substring(1).toLowerCase();
}

判断是否为字符串类型

function isString(str){
    return toString.call(str) === "[object String]";
}

判断是否为函数类型

function isFunction(foo){
	return toString.call(foo) === "[object Function]";
}

获取数据类型

function getDataType(data){
	return Object.prototype.toString.call(data).slice(8,-1).toLowerCase();
}

判断是否处于严格模式

function isStrictMode (){
	return !this;
}

返回只含有数字的字符串

function  containsOnlyNumber(str) {
    if ((/\d+(\.)?(\d)*/).test(str)) {
        return str.match(/\d+(\.)?(\d)*/)[0];
    }else{
        return "";
    }
}

滚动到页面的顶部或者底部

var position = {
    scrollToTop: function(){
        window.scroll(0, 0);
    },
    scrollToBottom: function(){
        window.scroll(0, document.documentElement.offsetHeight);
    }
}

将对象数组的所有键值都抽离出来放入一个新对象中:

var toObj = function(sArr){
	var obj = {};
	sArr.forEach(function(item, index){
		for(key in item){
			obj[key] = item[key];
		}
	});
	return obj;
}