Skip to content

Instantly share code, notes, and snippets.

@seia-soto
Last active July 16, 2020 08:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seia-soto/48ce03d2588b6baae08b3fa50d87bb8b to your computer and use it in GitHub Desktop.
Save seia-soto/48ce03d2588b6baae08b3fa50d87bb8b to your computer and use it in GitHub Desktop.
Simple binding function in JavaScript.
String.prototype.bind = function (parameters) {
let text = this
const keys = text.match(/\{(.*?)\}/g)
if (!keys) return this
for (let i = 0; i < keys.length; i++) {
const keyname = keys[i].replace('{', '').replace('}', '')
text = text.replace(keys[i], parameters[keyname] || '')
}
return text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment