Skip to content

Instantly share code, notes, and snippets.

View fredyang's full-sized avatar

Fred Yang fredyang

View GitHub Profile
@fredyang
fredyang / jquery.ba-simple-ajax-mocking.js
Created December 8, 2011 02:52 — forked from cowboy/jquery.ba-simple-ajax-mocking.js
Simple jQuery (1.5+) AJAX Mocking (requires JSON, tested in jQuery 1.7))
/*!
* Simple jQuery (1.5+) AJAX Mocking - v0.1.0 - 11/16/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($) {
@fredyang
fredyang / gist:1447252
Created December 8, 2011 15:15
check how many global variable is used in chrome
(function() {
var keys = "allKeys,top,window,location,external,chrome,v8Locale,document,SVGPathSegLinetoVerticalRel,SVGFESpotLightElement,HTMLButtonElement,Worker,webkitIDBTransaction,webkitNotifications,EntityReference,NodeList,screenY,SVGAnimatedNumber,SVGTSpanElement,navigator,MimeTypeArray,sessionStorage,SVGPoint,SVGScriptElement,OverflowEvent,HTMLTableColElement,OfflineAudioCompletionEvent,HTMLOptionElement,HTMLInputElement,webkitIDBIndex,SVGFEPointLightElement,SVGPathSegList,SVGImageElement,HTMLLinkElement,defaultStatus,MutationEvent,HTMLMetaElement,XMLHttpRequestProgressEvent,WebKitCSSTransformValue,Clipboard,HTMLTableElement,SharedWorker,SVGAElement,SVGAnimatedRect,webkitIDBDatabaseError,HTMLSpanElement,SVGGElement,toolbar,SVGLinearGradientElement,innerHeight,webkitIndexedDB,SVGForeignObjectElement,SVGAnimateElement,applicationCache,SVGFontElement,webkitAudioContext,pageXOffset,SVGFontFaceElement,ErrorEvent,Element,SVGPathSegCurvetoQuadraticSmoothRel,opener,SVGStopElement,HTMLUnknownElement,StyleShe
@fredyang
fredyang / silly-array-shuffle.js
Created December 8, 2011 15:16 — forked from cowboy/silly-array-shuffle.js
JavaScript: Silly Array Shuffle
// Array.indexed(4) returns [0, 1, 2, 3]
Array.indexed = function(n) {
var result = [];
while (n--) {
result[n] = n;
}
return result;
};
// Shuffle an array in a not too efficient way.
@fredyang
fredyang / userGlobal
Created December 20, 2011 20:35
display how many global variable is used by user (not system)
(function() {
var keys = [];
for (var k in window) {
keys.push(k);
}
function isUserGloba(k) {
for (var i = 0; i < keys.length; i++) {
if (keys[i] === k) {
@fredyang
fredyang / pipleline
Created February 6, 2012 05:55
help you simulate the pipeline in other functional language
function makepipe ( initValue ) {
var result = initValue,
slice = [].slice,
rtn = function( fn ) {
result = fn.apply( null, [result].concat( slice.call( arguments, 1 ) ) );
return rtn;
};
rtn.get = function() {
@fredyang
fredyang / gist:1865966
Created February 19, 2012 21:42
isAncestor and isDescendant
$.fn.isAncestor = function( elem ) {
var $elem = $( elem );
if (this.is( $elem )) {
return false;
}
while (($elem = $elem.parent()).length) {
if (this.is( $elem )) {
return true;
}
@fredyang
fredyang / ECMAScript5.js
Created February 27, 2012 02:05 — forked from think49/ECMAScript5.js
ECMAScript5.js : ES5 準拠のメソッドを定義するJavaScriptライブラリ
/**
* ECMAScript5.js (ECMA-262 Edition 5)
*
* @version 1.0
* @author think49
*/
// 15.4.3.2 Array.isArray ( arg )
//
// 1. If Type(arg) is not Object, return false.
@fredyang
fredyang / gist:2285344
Created April 2, 2012 17:20
add script
var script = document.createElement( 'script' );
script.src = "http://code.jquery.com/jquery-latest.js";
document.head.appendChild( script );
@fredyang
fredyang / gist:2297169
Created April 4, 2012 02:22
buildAccess
function buildAccess ( storage, convertKey, convertValue ) {
return function access ( key, value ) {
if (typeof key === "object") {
for (var k in key) {
access( k, key[k] );
}
return storage;
}
@fredyang
fredyang / gist:3970911
Created October 29, 2012 01:39
fool around the operator "intanceof"
var A = function () {};
var B = function () {};
//begin_trick
//---the following will make " (new B()) instanceof A " return true
(function () {
var T = function () {};
T.prototype = A.prototype;
B.prototype = new T();