<!DOCTYPE html> <html> <head> <title></title> <scriptsrc="string-format.js"></script> <script> console.log(formatString("I can speak {language} since I was {age}", {language: 'Javascript', age: 10})); console.log(formatString("I can speak {0} since I was {1}",'Javascript', 10)); console.log('I can speak {language} since I was {age}'.format({language: 'Javascript', age: 10})); console.log('I can speak {0} since I was {1}'.format('Javascript', 10)); console.log('Unreplaced {{id}}'.format(123)); </script> </head> <body>
</body> </html>
输出
I can speak Javascript since I was 10 I can speak Javascript since I was 10 I can speak Javascript since I was 10 I can speak Javascript since I was 10 Unreplaced {id}