Skip to content

Instantly share code, notes, and snippets.

@hbillings
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbillings/80b44fb4f686b19ca891 to your computer and use it in GitHub Desktop.
Save hbillings/80b44fb4f686b19ca891 to your computer and use it in GitHub Desktop.
Javascript refactor
function Article(){
stuff = arguments[1];
}
Article.prototype = {
// make this DRY-compatible
get_tags:function(path) {
tags = [];
for (word in stuff.split()) {
sometags = Tags.get_all(path);
for (tag in sometags) {
if (word == tag)
tags.push(word);
}
}
return tags;
}
}
// combine User & SystemTags into one abstract function
function Tags() {};
Tags.prototype = {
get_all:function(path) {
// hoist the variable so there's something to assign to & return
var result;
jQuery.ajax({
'url': path,
success: function(text) {
// then assignment can happen w/in ajax call
result = text.split(',');
}
});
// and result can be returned from larger function
return result;
}
}
Article.prototype.user_tags = Article.prototype.get_tags('/tags');
Article.prototype.system_tags= Article.prototype.get_tags('/systemtags');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment