Skip to content

Instantly share code, notes, and snippets.

View zhuochun's full-sized avatar

Zhuochun zhuochun

View GitHub Profile
@zhuochun
zhuochun / jQuery Plugin.js
Created October 28, 2012 15:47
jQuery Plugin Boilerplate
/* ========================================
* <Project> - <Description>
*
* Author:
* Last Edit:
* ========================================
* <License>
* ======================================== */
// the semi-colon before function invocation is a safety net against concatenated
@zhuochun
zhuochun / js template.js
Created October 28, 2012 15:46
RequireJS + jQuery
/* ========================================
* <Project> - <Description>
*
* Author:
* Last Edit:
* ========================================
* <License>
* ======================================== */
define(function(require, exports) {
@zhuochun
zhuochun / Underscore.js
Created October 22, 2012 09:04
Extend Methods in JavaScript
/*
-> http://underscorejs.org/#extend
extend(destination, *sources)
Copy all of the properties in the source objects over to the destination object, and return the destination object.
It's in-order, so the last source will override properties of the same name in previous arguments.
*/
@zhuochun
zhuochun / Convert to Number.js
Last active October 11, 2015 22:28
JavaScript Snippets from Everywhere
+1 === 1;
+0 === 0;
+undefined == NaN;
+null === 0;
+({1:2,2:3}).length == NaN
// Underscore -> https://github.com/documentcloud/underscore/blob/master/underscore.js#L80
@zhuochun
zhuochun / pubsub.md
Created July 23, 2012 07:44 — forked from addyosmani/pubsub.md
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)

#Four Ways To Do Pub/Sub With jQuery 1.7 and jQuery UI (in the future)

Between jQuery 1.7 and some of work going into future versions of jQuery UI, there are a ton of hot new ways for you to get your publish/subscribe on. Here are just four of them, three of which are new.

(PS: If you're unfamiliar with pub/sub, read the guide to it that Julian Aubourg and I wrote here http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx)

##Option 1: Using jQuery 1.7's $.Callbacks() feature:

$.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, but for more information on it (including lots of examples), see my post on $.Callbacks() here:

@zhuochun
zhuochun / core.test.js
Created July 12, 2012 14:13 — forked from drewwells/core.test.js
RequireJS and QUnit sitting in a tree
//Wait for relevant code bits to load before starting any tests
define(['core.js'], function( core ) {
module("Core Tests");
test("Test core methods", function(){
expect(2);
equals( 1, 1, "A trivial test");
ok( true, "Another trivial test");
});
@zhuochun
zhuochun / Singleton pattern
Created June 15, 2012 04:34
Singleton JavaScript Pattern
// http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html
var Singleton = (function() {
var instance = null;
function PrivateConstructor() {
var rand = Math.round(Math.random() * 100);
this.getRand = function() {
return rand;
}
(function () {
// ... all vars and functions are in this scope only
// still maintains access to all globals
}());
@zhuochun
zhuochun / example-user.js
Created June 14, 2012 14:53 — forked from nijikokun/example-user.js
Beautiful Validation... Why have I never thought of this before?!
var user = {
validateCredentials: function (username, password) {
return (
(!(username += '') || username === '') ? { error: "No Username Given.", field: 'name' }
: (!(username += '') || password === '') ? { error: "No Password Given.", field: 'pass' }
: (username.length < 3) ? { error: "Username is less than 3 Characters.", field: 'name' }
: (password.length < 4) ? { error: "Password is less than 4 Characters.", field: 'pass' }
: (!/^([a-z0-9_-]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' }
: false
);
@zhuochun
zhuochun / app.commonjs.js
Created June 14, 2012 13:12 — forked from kwhinnery/app.commonjs.js
Ti.include, browser, and CommonJS module
//CommonJS style
var mod = require('browser.ti.module');
mod.sayHello('Kevin');