Skip to content

Instantly share code, notes, and snippets.

@lifeinafolder
lifeinafolder / notoperatortrick2.js
Created December 6, 2010 19:46
Character Search in String using !~ operators
var str = 'posterous';
if ( !~str.search('t') ) {
// character 't' not found branch
}
else{
// found branch
}
var myAwesomeLibrary = {
_prop1 : '', //private property. should not be accessed externally
prop2 : '' //public property.
};
//public function of the library
myAwesomeLibrary.fn = function(){};
//function meant for internal usage. Should not be called from outside
myAwesomeLibrary._fn2 = function(){};
var myFunkyLibrary = (function(){
var _prop1 = ''; // this property turned private. woozahh!
//function meant for internal usage. This is truly private now.
var _fn2 = function(){};
return {
prop2 : 'some value here', //public property
fn = function(){} //public function of the library
};
})();
var myFunkyLibrary = function(){
var _prop1 = ''; // this property turned private. woozahh!
//function meant for internal usage. This is truly private now.
var _fn2 = function(){};
this.prop2 = 'some value here'; // a public property.
this.fn = function(){}; //public function of the library
};
//his own object literal
var $externalLib_Proto = {};
// aliasing your library to his object
myFunkyLibrary.call($externalLib_Proto);
//calling your library
$externalLib_Proto.fn();
@lifeinafolder
lifeinafolder / vanilla-observer-pattern.js
Created December 6, 2011 07:50
Textbook Observer Pattern Example
/**
* Extremely naiive implementation of Observer pattern
* Its used to illustrate the concept and to basically show
* How its done. This is not production level code.
* @author: Rajat Mittal
*/
/**
* Sample Model Constructor
* @param {Object} fields
@lifeinafolder
lifeinafolder / new-observer-pattern.js
Created December 6, 2011 07:54
Fun way of doing Observer pattern without maintaining an array of listeners and closely mimicing browser eventing scheme.
// @author: Rajat Mittal
/**
* Sample Model Constructor
* @param {Object} fields
*/
var Model = function(fields){
this._fields = fields;
/**
* NOTICE how we dropped the internal array _listeners
@lifeinafolder
lifeinafolder / promises.js
Created January 15, 2012 20:07
Promise Example
asyncCall(data,function(response){
// You have response from your asynchronous call here.
}, function(err){
// if the async call fails, the error callback is invoked
});
// And now with Promises pattern, you would write it like this:
var promise = asyncCall(data);
@lifeinafolder
lifeinafolder / jsdoc-async.js
Created January 15, 2012 20:10
Async Call JsDoc
/**
* asynchronousCall function
* @param data - input data
* @param success callback. Should accept `response` parameter
* @param failure callback. Should accept `error` parameter
* @return void
*/
@lifeinafolder
lifeinafolder / jsdoc-promise.js
Created January 15, 2012 20:11
promise jsdoc
/**
* asynchronousCall function
* @param data - input data
* @return promise object- It will resolve with `response` data
* or fail with `error` object
*/