您的位置:首页 > Web前端 > JavaScript

JAVASCRIPT常用函数集合

2017-03-28 14:21 507 查看
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)
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: