好的,所以我有这个函数,我只想分解它到底返回什么。
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
假设:c = 76
因此:
c.toString(16) = 4c
那么接下来要做什么?
最佳答案
它返回 Conditional (Ternary) Operator 的结果,它检查字符串 hex
的长度并返回 0
加上字符串 hex
或字符串 hex
。
长版本是这样的:
if (hex.length == 1) {
return "0" + hex;
} else {
return hex;
}
关于javascript - 函数中的这一行有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35409116/