'How to distinguish plain quoted string and template literal(backtick)
They are the same in the result, but they have different abilities. Is there any way to identify between them so that I can restrict the user(of a function) to use only template literal as an argument?
Solution 1:[1]
You mean something like an imaginary isFromTemplateLitteral boolean?
function(string){
if(string.isFromTemplateLitteral()){
// console.log("YES!")
}
}
No can do.
Because the string was passed to the function using reference in the memory stack for it. So even before the function body starts running, string is already a normal string. A primitive with type string.
There is no way to know how it has been constructed (template litteral, concatenation, simple/double quotes, etc.) or where it really comes from (user input, harcoded text, regular expression match, etc).
Solution 2:[2]
No, not really... just like you can't really differentiate 5 and 4 + 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Louys Patrice Bessette |
| Solution 2 | Evert |
