Skip to content

Instantly share code, notes, and snippets.

@zhannes
zhannes / index.html
Last active December 12, 2015 10:38
avoid globals, use a namespace
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<script src="js/module-1.js"></script>
<script src="js/module-2.js"></script>
</body>
@zhannes
zhannes / app.js
Last active December 12, 2015 04:38
rough touch based slider
(function($){
var slides, numSlides,
ct = 0,
settings = {
duration: 500,
easing: 'cubic-bezier(1.000, 0.000, 0.000, 1.000)'
};
function transition(){
var mod = (function(){
var foo; // set from outside
function setItem(val){
val && (foo = val);
}
return { setItem: setItem };
@zhannes
zhannes / dfd.js
Last active December 11, 2015 23:08
// assumes Q.js has been loaded and available as Q
function ajaxCall(){
var dfd = Q.defer(),
data = getData({
success: function(response){
dfd.resolve(response);
},
error: function(error){
dfd.reject(error);
}
@zhannes
zhannes / this.js
Last active December 10, 2015 18:58
`this` in javascript, scoping, execution context
/* how to bind `this` for callbacks, good example here too:
http://ejohn.org/apps/learn/#84
*/
var uiThing = {
parseResponse : function(data){
this.data = data;
this.renderUI()
},
renderUI : function(){ /* more code */ }
@zhannes
zhannes / mustache-helper-template.html
Created January 7, 2013 16:12
mustache.js helper functions - Add a property on your data object. Use the property name to reference the helper within your template. Inside the helper, you can reference the current object as `this`.
<script>
var people = getData(); // however you get your data ...
/* structure
{
people : [
{ name : "Shiva Khomini Somar Kondar Krohm" },
{ name : ... }
@zhannes
zhannes / gist:4259920
Created December 11, 2012 16:17
js inheritance example
/* for commented version, see https://gist.github.com/4259920/95d6d741d227c274be1ecf5238998c01f3658131 */
function object(o){
function ctor(){}
ctor.prototype = o;
return new ctor;
}
function inheritProto(subType,superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
@zhannes
zhannes / gist:4176977
Created November 30, 2012 16:56
javascript scoping - 3
/* #########################
3 - SCOPE - Define a function in a closure, return an object that has access to that function.
*/
(function(){
// accessible via the object we return
var o = (function(){
// local
function _f(){ _log( 'danneh' ); }
@zhannes
zhannes / gist:4176940
Created November 30, 2012 16:53
javascript scoping - 2
/* #########################
2 - SCOPE - CLOSURES
hide from global by using a closure
*/
(function(n){ // param
var myURL = 'google.com';
_log( n === 4 ) // true
@zhannes
zhannes / gist:4176909
Created November 30, 2012 16:49
javascript scoping - 1
// js scoping primer
// util
var _log = function(){
var slice = [].slice,
args = slice.call(arguments),
len = args.length,
i = 0;
for(;i<len;i++) console.log(args[i])
};