Home > front end >  How to distinguish plain quoted string and template literal(backtick)
How to distinguish plain quoted string and template literal(backtick)

Time:03-24

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?

CodePudding user response:

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).

CodePudding user response:

No, not really... just like you can't really differentiate 5 and 4 1.

  • Related