Skip to content

Instantly share code, notes, and snippets.

View fredyang's full-sized avatar

Fred Yang fredyang

View GitHub Profile
@fredyang
fredyang / gist:5457169
Last active December 16, 2015 15:39
guid generation
// Generate four random hex digits.
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
// Generate a pseudo-GUID by concatenating random hexadecimal.
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
};
// Console arguments testing
var apc = [].slice;
(function(){
console.log( apc.call(arguments) );
})( "false", 1, undefined, null, ["foo","bar","baz"], {a:1,b:2}, false );
(function(){
console.log.call( console, apc.call(arguments) );
// Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
// jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
// author: Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
@fredyang
fredyang / gist:5149224
Last active December 14, 2015 21:09
javascript base64
var base64 = (function () {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var obj = {
/**
* Encodes a string in base64
* @param {String} input The string to encode in base64.
*/
encode: function (input) {
var output = "";
<script type="text/ng-template" id="one.html">
<div>This is first template</div>
</script>
<script type="text/ng-template" id="two.html">
<div>This is second template</div>
</script>
@fredyang
fredyang / gist:3971215
Created October 29, 2012 02:54
subclass array
//http://dean.edwards.name/weblog/2006/11/hooray/
// create an <iframe>
var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
// write a script into the <iframe> and steal its Array object
frames[frames.length - 1].document.write(
"<script>parent.Array2 = Array;<\/script>"
);
@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();
@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: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 / 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.