Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
@Williammer
Williammer / jsMethod.borrowArray.js
Created June 16, 2014 07:51
jsMethod.borrowArray.js - borrow native methods of Array, ensure compatibility by not subclass Array.
function MyArray() {}
//MyArray.prototype = new Array(); //subclass Array, which is not compatible across all browsers.
MyArray.prototype.length = 0;
(function () {
var methods = ['push', 'pop', 'shift', 'unshift',
'slice', 'splice', 'join'];
for (var i = 0; i < methods.length; i++)(function (name) {
MyArray.prototype[name] = function () {
return Array.prototype[name].apply(this, arguments);
@Williammer
Williammer / jsPattern.OOPsubClass.js
Created June 16, 2014 09:11
jsPattern.OOPsubClass.js - an example of implementing subclass feature.
(function () {
var initializing = false,
superPattern =
/xyz/.test(function () { //check if supoort fucntion serialization.
xyz;
}) ? /\b_super\b/ : /.*/;
Object.subClass = function (properties) {
var _super = this.prototype;
initializing = true;
@Williammer
Williammer / js2jQuery.queryClassWithRegexp.html
Last active August 29, 2015 14:02
js2jQuery.queryClassWithRegexp.html - one example to find Class in selected elements with Regular expression. #note: 1. double escape slash "\\s" in RegExp constructor is just like the "\s" in literal regExp;
<div class="samurai ninja"></div>
<div class="ninja samurai"></div>
<div></div>
<span class="samurai ninja ronin"></span>
<script>
function findClassInElements(className, type) {
var elems = document.getElementsByTagName(type || "*");
var regex = new RegExp("(^|\\s)" + className + "(\\s|$)"); // match either the beginning of a string or whitespace; adn class name; and the end of string...
var results = [];
@Williammer
Williammer / jsRegExp.match.js
Created June 17, 2014 05:33
jsRegExp.match.js - pattern matches for string and tag, with local capturing feature with parentheses and global/local group match with exec in loop.
//// warm-ups
//var str = 'opacity=300';
//var reg = /opacity=([\d]{1,2})/;
//var res = (parseFloat(str.match(reg)[1]) / 100) + "";
//html tag match
var res = [];
var html = "<div class='test'><b>Hello</b> <i>world!</i></div>";
var tag = /<(\/?)(\w+)([^>]*?)>/g,
match;
@Williammer
Williammer / jsRegExp.replaceWithFn.js
Created June 17, 2014 08:46
jsRegExp.replaceWithFn.js - replace matched string with a callback function, with its parameters as "full match", "capturing 1st", "capturing 2nd" ... #note: using this technique with String.replace may be very useful.
function compress(source) {
var keys = {};
source.replace(
/([^=&]+)=([^&]*)/g,
function (full, key, value) {
keys[key] = (keys[key] ? keys[key] + "," : "") + value;
console.log(keys[key]);
return "";
@Williammer
Williammer / jsRegExp.trim.js
Created June 17, 2014 09:29
jsRegExp.trim.js - implement the trim function of string with both regExp and string functions, while string functions has poor performance.
String.prototype.trim = function () {
var reg = /^\s+|\s+$/g,
tr = function (full, hs, content, es) {
alert('full: ' + full + '; hs: ' + hs + '; content: ' + content + '; es: ' + es);
return content;
};
return this.replace(reg, '');
};
//trim with string functions, with very poor performance.
@Williammer
Williammer / jsTimer.centralTimerControl.html
Created June 18, 2014 07:21
jsTimer.centralTimerControl.html - implemented a timer that execute certain action and manage threads.
<body> < div id = "box" style = "position:relative;display:inline;" > Bird! </div>
</body>
<script>
var timers = {
timerID: 0,
timers: [],
add: function (fn) {
this.timers.push(fn);
},
start: function () {
@Williammer
Williammer / jsBasic.getPrototypeOf.js
Created July 3, 2014 08:28
jsBasic.getPrototypeOf.js - compatible version of ES5 getPrototypeOf method to find the prototype object of an instance.
if (typeof Object.getPrototypeOf === "undefined") {
Object.getPrototypeOf = function (obj) {
var t = typeof obj;
if (!obj || (t !== "object" && t !== "function")) {
throw new TypeError("not an object");
}
return obj.__proto__;
};
}
@Williammer
Williammer / jsBasic.secureConstructorFn.js
Created July 3, 2014 09:06
jsBasic.secureConstructorFn.js - defensively check the existence of the instantiate of constructor function to avoid calling it as function.
if (typeof Object.create === "undefined") {
Object.create = function (prototype) {
function C() {}
C.prototype = prototype;
return new C();
};
}
function User(name, passwordHash) {
var self = this instanceof User ? this : Object.create(User.prototype);
@Williammer
Williammer / jsPerform.nonblock.dynamicScript.js
Created July 7, 2014 02:28
jsPerform.nonblock.dynamicScript.js - dynamically create and load js file to deliver better performance.
function loadScript(url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};