简体   繁体   English

模板文字中带“”的参数和不带“”的参数有什么区别(js)

[英]What is difference between parameter with ' ' and parameter without ' ' in Template literals (js)

What is the difference between parameter with quotation and parameter without quotation in Template literals in JavaScript JavaScript中模板文字中带引号的参数和不带引号的参数有什么区别

Why when I put quotation >> ('${key}') it does the function >> fun('${key}') while when I delete quotation >> (${key}) it doesn't do the function >> fun(${key}) what different between parameter with quotation and parameter without quotation in Template literals with JavaScript为什么当我输入引号 >> ('${key}') 它执行 function >> fun('${key}') 而当我删除引号 >> (${key}) 它不执行 function >> fun(${key}) JavaScript 模板文字中带引号的参数和不带引号的参数有什么不同

for ( let key in states) { 
console.log(key);
var n =states[key].name;
var cur = states[key].currency;
console.log()

con.innerHTML += `<div> <span>name : "  ${n}
     " </span> <br> <span>cur : "  ${cur}
     " </span> </div><br> <span>cur : "  ${key}
     " </span>  <button onclick= fun('${key}')> select </button><hr> `
}

function fun(o) { 
alert(states[o].name);
alert(states[o].currency);
}

the code above run the function >> fun('${key}')上面的代码运行 function >> fun('${key}')

 for (let key in states) { console.log(key); var n = states[key].name; var cur = states[key].currency; console.log() con.innerHTML += `<div> <span>name: " ${n} " </span> <br> <span>cur: " ${cur} " </span> </div><br> <span>cur: " ${key} " </span> <button onclick= fun(${key})> select </button><hr> ` } function fun(o) { alert(states[o].name); alert(states[o].currency); }

It just adds ' in the final string.它只是在最后的字符串中添加'

The reason why this works in your example, but when you omit them it doesn't is because you are using that to generate js code inside an HTML handler.这在您的示例中起作用的原因,但是当您省略它们时它不起作用的原因是您使用它在HTML处理程序中生成js代码。

Your keys are strings, so in js they need to be enclosed in either ' or " . When you omit them they are treated as variables and most likely you don't have variables (or constants) with that name(s).您的键是字符串,因此在js中,它们需要包含在'"中。当您省略它们时,它们将被视为变量,并且很可能您没有具有该名称的变量(或常量)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模板文字中的换行符和字符串文字中的&#39;\\&#39;之间有什么区别? - What's the difference between line breaks in template literals and '\' in string literals? JavaScript 中的对象、Object 文字和模板文字有什么区别 - What is the difference between Objects, Object Literals and Template Literals in JavaScript 在方括号中指定参数与不指定方括号之间有什么区别? - What is the difference between specifying the parameter between brackets and without it? 模板文字和标记模板文字之间的区别? - Difference between Template literals and Tagged template literals? 有没有选择器参数和jquery委托的jquery有什么区别? - What is the difference between jquery on with / without selector parameter and jquery delegate? 这些对象字面量有什么区别? - What is the difference between these object literals? AngularJS函数,带和不带“ $”的参数之间的差异 - AngularJS Function, difference between parameter with and without '$' React - 作为 onClick 参数的 {doThisFunc}、{doThisFunc()} 和 {()=&gt;doThisFunc()} 有什么区别? - React - What's the difference between {doThisFunc} , {doThisFunc()} and {()=>doThisFunc()} as onClick parameter? 具有解构语法的渲染函数与具有纯空参数的渲染函数之间有什么区别? - What is difference between render function with destructing syntax and with plain empty parameter? Ajax成功函数参数数据和响应之间有什么区别? - What is the difference between ajax success function parameter data and response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM