如何控制日志变量/函数的名称? [英] How to console log the name of a variable/function?

查看:52
本文介绍了如何控制日志变量/函数的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做: log(variableOrFunction)

并且能够产生: variableOrFunction:actualValue

我试过这个:

export const log = (value) => {
  console.log('' + value + ':', value)
  console.log(value)
}

但我得到的是: [object Object]:

这样做的正确性是什么?

What's the correct of doing this?

推荐答案

你可以用它们周围的一对括号记录它们( {} ),它将创建一个以变量名称为键的对象:

You can log them with a pair of braces around them ({}), which will create an object with the name of the variable as the key:

function someFunction() {};

const someOtherFunction = () => {};

const someValue = 9;

console.log({someFunction});
console.log({someOtherFunction});
console.log({someValue});

const renamed = someFunction;

console.log({renamed})

这篇关于如何控制日志变量/函数的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆