javascript - 如何让我的函数找到我的输入参数?
问题描述
我正在创建一个函数,该函数将使用预定义的样式格式化我的输入文本。
const icons = {
user: {'icon_cod': '%c %c', 'font_icon': 'font-family:"Font Awesome 5 Free"; font-weight: 900;'} // declare my icon
}
const txt_styles = {
texto: {'style': 'color:red; font-family: "Open Sans"; font-weight: 700; font-size: 15px;', // Format my text and icon
'icon_style': 'color:blue;'}
}
function icon_construct(icon_chosen,text,style) {
txt_formatted = ''
if (icon_chosen in icons && style in txt_styles) { // Check the input
txt_formatted = icons.icon_chosen['icon_cod'] + text, icons.icon_chosen['font_icon'] + txt_styles.style['icon_style'], txt_styles.style['style']
} return txt_formatted // Returns formatted text
}
console.log(icon_construct('user','Hello World','body'));
但是退出是向我返回此错误:
预期的输出将是这个
举例说明格式化是如何发生的
//Run in the browser console
console.log ('%c Red %c Blue','color: red; ','color: blue; ')
样式遵循元素声明的顺序 函数未找到参数的值
解决方案
试试这个(请注意,代码片段无法将设计打印到控制台,请查看 devtools 以查看结果):
const
icons = {
user: {
iconCode: '%c %c',
fontIcon: 'font-family: "Font Awesome 5 Free"; font-weight: 900;'
}
},
textStyles = {
text: {
style: 'color: red; font-family: "Open Sans"; font-weight: 700; font-size: 15px;',
iconStyle: 'color: blue;'
}
},
iconConstruct = (iconChosen, text, style) =>
iconChosen in icons && style in textStyles ? [
icons[iconChosen].iconCode + text,
icons[iconChosen].fontIcon + textStyles[style].iconStyle,
textStyles[style].style
] : [];
console.log(...iconConstruct('user', 'Hello World', 'text'));
推荐阅读
- opencv - cv 文件存储读取 yaml 未指定“%YAML:1.0”行
- python - Pandas: Speeding up many string searches
- javascript - 在 Sequelize Javascipt 中建立关系
- .net - Visual Studio:重建解决方案时是否会恢复 nuget 包?
- firebase - firebase 未收到某些事件
- bison - 在 Bison 中将属性从一个孩子传递给另一个孩子
- r - 如何在观星者中排除交互项?
- php - 如何解决连续的 wordpress 重定向和注入黑客攻击?
- flutter - Flutter webview中的多个选项卡
- pytorch - DistilBERT 损失梯度用于衡量代币重要性