我想將我的console.log()消息抽象爲一個變量。這裏是代碼:如何用消息正確抽象console.log顏色變量
我正在使用console.log顏色消息。
console.log("%c Scenario 1.0:" + "%c [street number] + [direction] + [street name] + [suffix] + anything else", console.colors.bold.yellow, console.colors.white);
這導致
場景1.0:(粗體和黃色)
[街道號碼] + [方向] + [街道名稱] + [後綴] +別的(正常和白色)
console.colors = {
"gray": "color: #1B2B34;",
"red": "color: #DB4C4C;",
"orange": "color: #F99157;",
"yellow": "color: #BADA55;",
"green": "color: #99C794;",
"teal": "color: #5FB3B3;",
"blue": "color: #6699CC;",
"purple": "color: #C594C5;",
"black": "color: #000000;",
"white": "color: #FFFFFF;",
"brown": "color: #AB7967;",
}
console.colors.bold = {
"gray": "font-weight: bold;" + console.colors.gray,
"red": "font-weight: bold;" + console.colors.red,
"orange": "font-weight: bold;" + console.colors.orange,
"yellow": "font-weight: bold;" + console.colors.yellow,
"green": "font-weight: bold;" + console.colors.green,
"teal": "font-weight: bold;" + console.colors.teal,
"blue": "font-weight: bold;" + console.colors.blue,
"purple": "font-weight: bold;" + console.colors.purple,
"black": "font-weight: bold;" + console.colors.black,
"white": "font-weight: bold;" + console.colors.white,
"brown": "font-weight: bold;" + console.colors.brown
}
var caseConsoleLogColors = "console.colors.bold.yellow, console.colors.white";
var scenario = {
case1_0: "%c Scenario 1.0:" + "%c [street number] + [direction] + [street name] + [suffix] + anything else", caseConsoleLogColors,
case1_1: "%c Scenario 1.1:" + "%c [street number] + [direction] + [street name]", caseConsoleLogColors,
case2: "%c Scenario 2:" + "%c [street number] + [street name] + [suffix] - No cardinal direction, 3 items or more", caseConsoleLogColors,
case3: "%c Scenario 3:" + "%c [street number] + [numeric street name]", caseConsoleLogColors,
case4_0: "%c Scenario 4.0:" + "%c [street number] + [alphabet street name]", caseConsoleLogColors,
case4_1: "%c Scenario 4.1 CONFLICT:" + "%c [street name] + [suffix]", caseConsoleLogColors,
case5: "%c Scenario 5.0:" + "%c [direction] + [numeric street name]", caseConsoleLogColors,
case6: "%c Scenario 6:" + "%c [direction] + [numeric street name] + [suffix] + anything else", caseConsoleLogColors
}
// works great
console.log("%c Scenario 1.0:" + "%c [street number] + [direction] + [street name] + [suffix] + anything else", console.colors.bold.yellow, console.colors.white);
// does not work
console.log("%c Scenario 1.0:" + "%c [street number] + [direction] + [street name] + [suffix] + anything else", caseConsoleLogColors);
// does not work
console.log(scenario.case1);
這一切都很好。問題是,我想抽象消息和顏色出的console.log()和成一個變量名,這樣我就可以撲通的變量中,像這樣的
console.log(scenario.case1_0)
不過的console.log着色和消息中斷。它不輸出正確的信息或顏色。我如何正確抽象?
查看JSbin您的瀏覽器控制檯打開:被傳遞到日誌 https://jsbin.com/duxefogufo/1/edit?js,output
謝謝,我會嘗試了這一點,看看它是如何去和更新很快。 – TetraDev