跳到主要内容

作用

绑定到控制台

export function bindToConsole(
methodName: string,
args: Array<any>,
badgeName: string,
): () => any {
let offset = 0;
switch (methodName) {
case 'dir':
case 'dirxml':
case 'groupEnd':
case 'table': {
// These methods cannot be colorized because they don't take a formatting string.
// 这些方法不能被着色,因为它们不接受格式化字符串。
return bind.apply(console[methodName], [console].concat(args));
}
case 'assert': {
// assert takes formatting options as the second argument.
// assert 将格式化选项作为第二个参数。
offset = 1;
}
}

const newArgs = args.slice(0);
if (typeof newArgs[offset] === 'string') {
newArgs.splice(
offset,
1,
badgeFormat + ' ' + newArgs[offset],
pad + badgeName + pad,
);
} else {
newArgs.splice(offset, 0, badgeFormat, pad + badgeName + pad);
}

// The "this" binding in the "bind";
// “bind”中的“this”绑定;
newArgs.unshift(console);

return bind.apply(console[methodName], newArgs);
}

常量

徽章格式

备注

源码中 10 - 14 行

// Keep in sync with ReactServerConsoleConfig
// 与 ReactServerConsoleConfig 保持同步
// 徽章格式
const badgeFormat = '[%s]';
// 填充
const pad = ' ';

// 绑定
const bind = Function.prototype.bind;