javascript是否有一个“repr”等价物?
我最接近Python的repr是:javascript是否有一个“repr”等价物?,javascript,python,node.js,Javascript,Python,Node.js,我最接近Python的repr是: function User(name, password){ this.name = name; this.password = password; } User.prototype.toString = function(){ return this.name; }; var user = new User('example', 'password'); console.log(user.toString(
function User(name, password){
this.name = name;
this.password = password;
}
User.prototype.toString = function(){
return this.name;
};
var user = new User('example', 'password');
console.log(user.toString()) // but user.name would be even shorter
是否有一种方法可以在默认情况下将对象
表示为字符串?或者我必须只使用object.variable
来获得我想要的结果吗?可能是您从本机库获得的最接近的结果。它不能很好地处理对象,但是您可以定义自己的代码来解决这个问题。我搜索了提供此功能的库,但没有找到任何内容
String(user)
这是我能想到的最好的了。我认为另一种选择可能是找到一个第三方库,用于为对象创建人类可读的表示。Node.js
util.inspect
要获取终端上显示为字符串的对象表示,可以执行以下操作:
const util = require('util');
console.log(util.inspect({ a: "0\n1", b: "c"}));
// Equivalent: automatically called by `console.log`.
console.log({ a: "0\n1", b: "c"});
输出:
{ a: '0\n1', b: 'c' }
{ a: '0\n1', b: 'c' }
这与将字符串{a:“0\n1”,b:“c”}
复制粘贴到终端并按enter键时在终端上显示的相同
为自定义表示重写类的inspect
方法
这是在:被问到的,在:被提到的,但这里有一个更完整的例子:
const util = require('util');
class MyClass {
constructor(a, b) {
this.a = a;
this.b = b;
}
[util.inspect.custom]() {
return `a is ${this.a} and b is ${this.b}`;
}
}
const my_object = new MyClass(1, 2);
console.log(util.inspect(my_object));
console.log(my_object);
输出:
a is 1 and b is 2
a is 1 and b is 2
如果我们没有定义自定义[util.inspect.custom]
的话,默认的检查应该是:
MyClass { a: 1, b: 2 }
如果直接检查终端上的对象,也会显示相同的表示:
> my_object
a is 1 and b is 2
在较旧的Node.js中,语法通常是:
inspect(){}
但这是不赞成的消息:
[DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
如所述:
util.使用换行符和缩进检查
不可能?叹息:
在Node.js v10.15.1中测试。在Nodejs中,
console.log
可以通过向Javascript对象添加inspect()
方法来覆盖该对象
例如:
-感谢“西罗·桑蒂利”正如安德鲁·约翰逊所说,
JSON.stringify
可能是最接近实际的版本
repr的一个常见策略是。如果您想这样做,(与eval
相反)是一个不错的选择
例如:
var escodegen = require('escodegen')
var lave = require('lave')
function User(name, password){
this.name = name;
this.password = password;
}
var user = new User('example', 'password');
console.log(lave(user, {generate: escodegen.generate}));
输出(不像我希望的那样优雅!)
对我来说,一个快捷方式是用数组文字包装值,如下所示:
console.log([variable]);
浏览器开发人员控制台中的输出非常清楚地表明数组的唯一元素是什么
这是针对NodeJ的解决方案(不确定浏览器)。如上所述,您可以将
inspect(depth,opts)
添加到类中,当您console.log(user\u class\u实例)时将调用它代码>
因此,这应该做到:
User.prototype.inspect = function(depth, opts){
return this.name;
};
NodeV6.6.0引入了符号:它是一个全局注册的符号,可通过symbol.for('nodejs.util.inspect.custom')
访问。它可用于声明自定义检查函数
下面是OP案例的使用示例:
函数用户(名称、密码){
this.name=名称;
this.password=密码;
此[Symbol.for('nodejs.util.inspect.custom')]=()=>this.name;
}
var user=new user('example','password');
console.log(用户)/“示例”
console.log(user+)
console.log(String(user))
@Epascarello这两种方法都有效,因此如果没有其他人回答,并且这是唯一的两种方法,我建议您将其作为答案发布,以便获得学分。我希望它们是另一种选择。您是否特别询问了一种等价于dict.\uuuu repr\uuuu
?您可以随时编写自己的repr
函数来查找\uu repr\uuu
方法您知道哪些好方法吗?但我想这是必须的,不是我想的。原型路由也不是一个坏主意。您需要var util=require('util')代码>@richard谢谢你的建议。出于某种原因,我不明白它在我当时测试过的节点0.10.26上不需要就可以正常工作,但我也不确定这在当时是否是个好主意。看起来inspect
比JSON.stringify
,信息量更大,因为它包含构造函数名称。我认为它不再有效了,至少在Chrome 79.0.3945.117I版本中没有提到NodejsIt。请注意,重写inspect
已被弃用,现在您必须实现[util.inspect.custom]
:
console.log([variable]);
User.prototype.inspect = function(depth, opts){
return this.name;
};