Skip to content

Instantly share code, notes, and snippets.

View xuanfeng's full-sized avatar

ivan xuanfeng

View GitHub Profile
@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 / 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 / jsModule
Created January 13, 2014 10:07
JS 模块化写法
// 匿名函数-实现模块化
(function(){
// 定义对象
var Draw = {};
// 共有方法
Draw.init = function(){};
// 私有方法
Draw._private = function(){};
// 外部接口
@xuanfeng
xuanfeng / animal.txt
Last active August 29, 2015 14:05 — forked from edokeh/index.js
/*code is far away from bug with the animal protecting
* ┏┓   ┏┓
*┏┛┻━━━┛┻┓
*┃       ┃  
*┃   ━   ┃
*┃ ┳┛ ┗┳ ┃
*┃       ┃
*┃   ┻   ┃
*┃       ┃
*┗━┓   ┏━┛
@xuanfeng
xuanfeng / randomWord.js
Last active December 8, 2019 09:37
产生任意长度随机字母数字组合
/*
** randomWord 产生任意长度随机字母数字组合
** randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
** xuanfeng 2014-08-28
*/
function randomWord(randomFlag, min, max){
var str = "",
range = min,
arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
@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 / 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 / htmlDecode.js
Last active August 29, 2015 14:09
html转码
function htmlDecode(str){
return str
.replace(/&#39;/g, '\'')
.replace(/<br\s*(\/)?\s*>/g, '\n')
.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&');
@xuanfeng
xuanfeng / url_analyse.js
Last active August 29, 2015 14:12
url参数
/*
* 判断URL中是否有此参数
*/
hasQueryStringRegExp: function(url, name){
var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
if(reg.test(url)){
return true
}else{
return false
}