Skip to content

Instantly share code, notes, and snippets.

@HenrikJoreteg
Created October 22, 2010 21:20
Show Gist options
  • Save HenrikJoreteg/641397 to your computer and use it in GitHub Desktop.
Save HenrikJoreteg/641397 to your computer and use it in GitHub Desktop.
Rather than creating some other util global, just extend underscore.js with any additional methods you want.
// If you don't use underscore.js, use it (http://documentcloud.github.com/underscore/)
// Then, use underscore's mixin method to extend it with all your other utility methods
// like so:
_.mixin({
escapeHtml: function () {
return this.replace(/&/g,'&')
.replace(/>/g,'>')
.replace(/</g,'&lt;')
.replace(/"/g,'&quot;')
.replace(/'/g,'&#39;');
},
otherFunc: function () {
// etc, etc.
}
});
// simple. but. handy.
@jdalton
Copy link

jdalton commented Sep 15, 2012

Yap, @krunkasaurus (lol, I love the name) is right, Underscore already has _.escape and the edge version has _.unescape too.

You can also use Lo-Dash, a drop-in replacement for Underscore delivering performance, bug fixes, and additional features to make a custom build and embed it directly in your project like (BonsaiJS)[http://bonsaijs.org/].

@kjantzer
Copy link

perfect! just what I was looking for. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment