JAVASCRIPT常用函数集合

1、删除数组某项
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};


2、当前时间

function getNow() {
	var date = new Date();
	return date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}


3、格式化日期
function formater(date) {
	return date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + date.getDate();
}


4、判断是否JSON格式对象
function isJson(obj) {
	return typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
}


5、canvas的相对window窗口坐标转换成canvas内部相对坐标
function windowToCanvas(canvas, x, y) {
   var bbox = canvas.getBoundingClientRect();
   return { x: x - bbox.left * (canvas.width  / bbox.width),
            y: y - bbox.top  * (canvas.height / bbox.height)
          };
}

你可能感兴趣的:(JavaScript)