Created
October 29, 2011 17:30
-
-
Save srhyne/1324823 to your computer and use it in GitHub Desktop.
JS Perf Notes closures & anon functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// avoid using anon function cb's for high touch events | |
$("div").mousemove(function(){ | |
//this function is re-created every time w/ obvious overhead | |
}); | |
//avoid re-creating Class methods in a constructor pattern. Use prototype instead | |
//instead of this... | |
function Person(first, last){ | |
this.first_name = first; | |
this.last_name = last; | |
//methods | |
this.getFullName = function(){ | |
return this.first_name+ " " + this.last_name; | |
} | |
} | |
//put getFullName in the prototype | |
Person.prototype.getFullName = function() { | |
return this.first_name+ " " + this.last_name; | |
}; | |
// I would say the module pattern with private vars is fine when | |
// initiating a class but for Factory'esque modules/classes reused a lot avoid closures | |
// If you need private vars for these high touch classes just use this._ convention |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment