If I have a function that returns a tagged template literal I can console log the function toString() and it will show me the object and property name of my variable.
function myfN() {
return tag`Test ${data.name}`
}
// this console logs the information I would need inside my tag, see below.
console.log(myfN.toString())
console output >>>>
function myfN() {
return tag(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["Test ", ""])), data.name);
}
as you can see data.name
is in the log result, now when we look inside my tag function I have my strings and values
function tag(literals, ...vars) {
console.log(literals.raw);
console.log(vars);
// get here that the literals are using `data.name`
}
Is there any way to get the variable names inside my tag without passing the whole object as a value into the string? Everything
CodePudding user response:
As with other functions, you can't access variable name that is used to pass data into it. There also may be no variable at all:
tag`Test ${"look, ma! No variables!"}`
Only way to get variable name (if there any) and use it would be parsing and manipulating code, for example with babel.