Last active
July 16, 2020 08:20
-
-
Save seia-soto/48ce03d2588b6baae08b3fa50d87bb8b to your computer and use it in GitHub Desktop.
Simple binding function in JavaScript.
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
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