Skip to content

Instantly share code, notes, and snippets.

@adeleinr
Last active December 28, 2015 13:09
Show Gist options
  • Save adeleinr/8e65076d07824e0c311e to your computer and use it in GitHub Desktop.
Save adeleinr/8e65076d07824e0c311e to your computer and use it in GitHub Desktop.
Javascript Patterns
BINDING
=======
1) Avoiding nesting function by using scope support
You are passing 'this' so that the funcion has
access to this function's context
app.mavenRecipeCollection.getMavenRecipeByMeal(e.slug, e.mealChoice,
this.onSuccess_, this);
2) With nesting function
You dont pass this but bz the function is anonymous it has access
to the context
app.mavenRecipes.get(arg1, arg2, function() {
});
3) Avoiding nesting function without scope support
You create a handler and pass 'this' to it
// Adding scope myself.
var successHandler = _.bind(this.successhandler_, this);
app.mavenRecipeCollection.getMavenRecipeByMeal(e.slug, e.mealChoice,
successhandler);
4) Example with an object literal.
You create a scope with the specific fields from 'this'
you need and pass it to the funcion
var test = function() {
console.log(this.name);
};
var thisScope = {
name: 'adelein'
};
_.bind(test, thisScope);
test();
5) All local variables at the top before the instance vars to avoid hoisting
Eg.
var x = 1;
this.y = 10;
6) HTML hooks use dashes, css too
7) Private methods use _ at the end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment