Skip to content

Instantly share code, notes, and snippets.

View blankyao's full-sized avatar
🎉
Happy Writing

Blank Yao blankyao

🎉
Happy Writing
View GitHub Profile
@blankyao
blankyao / gist:785647
Created January 19, 2011 03:38
shuffle an array in javascript
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
shuffle = function(o){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
@blankyao
blankyao / gist:828798
Created February 16, 2011 03:22
原型继承的实现
/* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.
*/
// Inspired by base2 and Prototype
(function(){
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function(){};
@blankyao
blankyao / gist:833352
Created February 18, 2011 06:55
A simple helper that allows you to bind new functions to the prototype of an object
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
};
@blankyao
blankyao / event.js
Created February 24, 2011 03:30
javascript事件处理封装
// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = addEvent.guid++;
@blankyao
blankyao / gist:850360
Created March 2, 2011 02:31
string.trim()
String.prototype.trim = function() {
var str = this,
whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for (var i = 0,len = str.length; i < len; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i >= 0; i--) {
@blankyao
blankyao / gist:915066
Created April 12, 2011 06:53
make sure that the new operator is always used
//http://ejohn.org/apps/learn/#36
function User(first, last){
if ( !(this instanceof arguments.callee) )
return new User(first, last);
this.name = first + " " + last;
}
@blankyao
blankyao / gist:940245
Created April 25, 2011 07:00
addDOMLoadEvent
/*
* (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
* Special thanks to Dan Webb's domready.js Prototype extension
* and Simon Willison's addLoadEvent
*
* For more info, see:
* http://www.thefutureoftheweb.com/blog/adddomloadevent
* http://dean.edwards.name/weblog/2006/06/again/
* http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
* http://simon.incutio.com/archive/2004/05/26/addLoadEvent
(function() {
var Central = window.Central = {};
var centralService = function(target) {
var listeners = {};
target.listen = function(command, handler) {
listeners[command] = listeners[command] || [];
var i = 0;
while (i < listeners[command].length && listeners[command][i] != handler) {
@blankyao
blankyao / jquery.floating.js
Created May 26, 2011 01:52
jQuery Floating Widget in javascript
(function ($) {
$.fn.floatingWidget = function () {
return this.each(function () {
var $this = $(this),
$parent = $this.offsetParent(),
$window = $(window),
top = $this.offset().top - parseFloat($this.css('marginTop').replace(/auto/, 0)),
bottom = $parent.offset().top + $parent.height() - $this.outerHeight(true),
floatingClass = 'floating',
pinnedBottomClass = 'pinned-bottom';
@blankyao
blankyao / gist:992538
Created May 26, 2011 04:12
makeClass By John Resig
// makeClass - By John Resig (MIT Licensed)
function makeClass(){
return function(args){
if ( this instanceof arguments.callee ) {
if ( typeof this.init == "function" )
this.init.apply( this, args.callee ? args : arguments );
} else
return new arguments.callee( arguments );
};
}