Skip to content

Instantly share code, notes, and snippets.

View xuanfeng's full-sized avatar

ivan xuanfeng

View GitHub Profile
@xuanfeng
xuanfeng / dateFormat.js
Last active November 9, 2015 06:09
时间格式化(年、月、日、时、分、秒)、获取时间区间
// 时间格式化
// 使用:var d = new Date().format('yyyy-MM-dd');
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
@xuanfeng
xuanfeng / inherit.js
Last active January 3, 2016 02:39
通过原型继承创建一个新对象
function inherit(p){
if(p ==null) throw TypeError();
if(Object.create){
return Object.create(p);
}
var t = typeof p;
if(t !== "object" && t !== "function") throwTypeError();
function f(){};
f.prototype = p;
return new f();
@xuanfeng
xuanfeng / extend.js
Last active January 3, 2016 02:39
extend()函数:用一个或多个其他对象来扩展一个对象,返回被扩展的对象
// 定义一个扩展函数,用来将第二个以及后续参数复制只第一个参数
// 这里我们处理了IE bug:在多数IE版本中
// 如果o的属性拥有一个不可枚举的同名属性,则for/in循环
// 除非我们先是检测它
var extend = (function(){ //将这个函数的返回值赋值给extend
// 在修复它之前,首先检查是否存在bug
for(var p in {toString: null}){
return function extend(o){
// 如果代码执行到这里,那么for/in循环会正确工作并返回
// 一个简单版本的extend()函数
@xuanfeng
xuanfeng / jsModule
Created January 13, 2014 10:07
JS 模块化写法
// 匿名函数-实现模块化
(function(){
// 定义对象
var Draw = {};
// 共有方法
Draw.init = function(){};
// 私有方法
Draw._private = function(){};
// 外部接口
@xuanfeng
xuanfeng / getSystemInfo.js
Last active January 3, 2016 02:48
getSystemInfo: 获取系统信息函数
// get system info
var getOSInfo = function () {
var ver = navigator.userAgent;
var OS = navigator.platform;
if(OS == "Win32" || OS == "Windows") {
return 'Windows';
} else if(OS == "Mac68K" || OS == "MacPPC" || OS == "Macintosh" || OS == "MacIntel") {
return "Mac";
} else if(OS == "X11") {
return "Unix";
@xuanfeng
xuanfeng / dabblet.css
Created May 21, 2016 10:59
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
background: #f06;
background: linear-gradient(45deg, #f06, yellow);
min-height: 100%;
@xuanfeng
xuanfeng / moduleWrap.js
Created July 13, 2016 13:17
define module
// js库模版
// 可直接被引用或者AMD、CMD方式加载
;(function(root, factory) {
if (typeof module !== 'undefined' && module.exports) {// CommonJS
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {// AMD / RequireJS
define(factory);
} else {
root.Promise = factory.call(root);
}
@xuanfeng
xuanfeng / ASCII Art
Created October 8, 2016 16:30
ASCII Art
// http://chris.com/ascii/index.php?art=video%20games/pokemon
quu..__
$$$b `---.__
"$$b `--. ___.---uuudP
`$$b `.__.------.__ __.---' $$$$" .
"$b -' `-.-' $$$" .'|
". d$" _.' |
`. / ..." .' |
`./ ..::-' _.' |
/ .:::-' .-' .'
@xuanfeng
xuanfeng / passwordLevel.js
Last active November 20, 2016 06:49
密码等级:至少包含字母、大小写数字、字符中的两种
function passwordLevel(password) {
var Modes = 0;
for (i = 0; i < password.length; i++) {
Modes |= CharMode(password.charCodeAt(i));
}
return bitTotal(Modes);
//CharMode函数
function CharMode(iN) {
if (iN >= 48 && iN <= 57)//数字
@xuanfeng
xuanfeng / util.js
Created August 14, 2017 12:26
util
_.type = function (obj) {
return Object.prototype.toString.call(obj).replace(/\[object\s|\]/g, '')
}
_.isArray = function isArray (list) {
return _.type(list) === 'Array'
}
_.slice = function slice (arrayLike, index) {