参数与 ' 之间有什么区别? '和参数不带 ' '在模板文字 (js) 中

发布于 2025-01-11 02:13:37 字数 1333 浏览 0 评论 0原文

JavaScript 中的模板文字中带引号的参数和不带引号的参数有什么区别

为什么当我把引号放在>>时? ('${key}') 它的功能是>> fun('${key}') 而当我删除引用时 >> (${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);
}

上面的代码运行函数>>有趣('${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);
}

What is the difference between parameter with quotation and parameter without quotation in Template literals in 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

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}')

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2025-01-18 02:13:37

它只是在最终字符串中添加 '

这在您的示例中有效,但当您省略它们时却不起作用,因为您正在使用它在 HTML 处理程序内生成 js 代码。

您的键是字符串,因此在 js 中,它们需要括在 '" 中。当您省略它们时,它们将被视为变量,并且很可能您没有具有该名称的变量(或常量)。

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文