如何在警报框中检查对象?通常警告对象只会抛出节点名称:
alert(document);
Run Code Online (Sandbox Code Playgroud)
但我想在警告框中获取对象的属性和方法.如果可能,我该如何实现此功能?或者还有其他建议吗?
特别是,我正在寻找一个生产环境的解决方案,其中console.log和Firebug不可用.
如何在nodejs调试控制台中更改对象实例的字符串表示形式.有没有一种方法(比如toString()
在.NET中)我可以覆盖?
请考虑以下代码:
class SomeObject{
constructor(){
this._varA = "some text";
this._varB = 12345;
this._varC = "some more text";
this._varD = true;
this._varE = 0.45;
}
toString(){
return "custom textual rapresentation of my object";
}
}
var array = [];
array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());
console.log(array);
Run Code Online (Sandbox Code Playgroud)
但是在我工作的其他环境和编程语言中,覆盖该toString()
方法将显示toString()
(在上面的示例中"custom textual representation of my object"
)的结果,而不是调试器创建的动态文本表示(在上面的示例代码中是SomeObject {_varA: "some text", _varB: 12345, _varC: "some more text", …}
:) - 我不知道如果没有定义自定义替代方案,那么在一分钟内它是非常有用的.
我也意识到这console.log(array.toString());
甚至console.log(array.map(t=>t.toString()));
会产生类似于我所追求的东西,但是这会阻止我使用调试导航来浏览对象,即.钻入对象图.
如果这是不可能的,其他人会从中受益吗?如果有足够的兴趣,我可以考虑定义和实现它作为一个功能.
javascript debugging node.js visual-studio-code vscode-debugger
似乎__repr__
函数可以返回不同的方式.
我有一个InfoObj类,它存储了很多东西,其中一些我并不特别希望自己设置类的用户.我意识到没有什么蟒蛇是受保护的,他们可能只是潜水,并设置也无妨,但似乎在定义它__init__
使得它更可能有人会看到它,并假设它是罚款,只是传递进去.
(示例:当确定对象已完全填充时由验证函数设置的布尔值,以及存储足够信息时从其他值计算的值...例如A = B + C,所以一次设置A和B,然后计算C并将对象标记为Valid = True.)
那么,考虑到所有这些,哪个是设计__ repr__输出的最佳方法?
bob = InfoObj(Name="Bob")
# Populate bob.
# Output type A:
bob.__repr__()
'<InfoObj object at 0x1b91ca42>'
# Output type B:
bob.__repr__()
'InfoObj(Name="Bob",Pants=True,A=7,B=5,C=2,Valid=True)'
# Output type C:
bob.__repr__()
'InfoObj.NewInfoObj(Name="Bob",Pants=True,A=7,B=5,C=2,Valid=True)'
Run Code Online (Sandbox Code Playgroud)
...类型C的要点是不愉快地将我在C++中设置为"私有"的所有东西作为构造函数的参数,并让使用该类的队友使用接口函数设置它,即使它更多的工作对他们来说 在这种情况下,我将定义一个不带某些东西的构造函数,以及一个稍微难以注意到的单独函数,用于__repr__
如果它有所不同,我打算使用它们的__repr__
输出将这些python对象存储在数据库中并使用它们进行检索eval()
,至少除非我想出更好的方法.队友手动创建完整对象而不是通过正确的接口函数的结果只是一种类型的信息检索可能不稳定,直到有人弄清楚他做了什么.
尝试更新MongoDB文档获取弃用警告为
(节点:71307)[DEP0079]弃用警告:不推荐使用.inspect()对象的自定义检查功能
节点版本v10.5.0,db版本v3.6.5,Mongoose版本mongoose@4.1.12
Campground.findById(campgroundId, function(err, campground){
if(err){
console.log(err);
} else {
console.log(campground.celebrity);
Celebrity.create(celebrityData, function(err, celebrity){
if(err){
console.log(err);
} else {
//save comment
celebrity.save();
campground.celebrity.push(celebrity);
campground.save();
console.log(celebrity);
//req.flash('success', 'Created a comment!');
}
});
}
});
Run Code Online (Sandbox Code Playgroud) 我想将JSON.stringify()
'd 对象打印到控制台,作为 Mocha 测试套件输出的一部分。
在测试缩进时,我希望对象日志行向右缩进足够远(例如,3-4 个制表符空格),以便它们在正确的describe()
组中可识别。
我怎么能用像console.log
或这样的东西来实现这一目标process.stdout.write
?
我想获得JavaScript中任何对象或值的字符串表示.我做了几个实验.
> var a = document.createTextNode('foo'); a
"foo"
> var a = document.createTextNode('foo'); a.toString()
"[object Text]"
> var a = 1; a.toString()
"1"
> (1).toString()
"1"
> 1.toString()
SyntaxError: Unexpected token ILLEGAL
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
1.toString()
失败?function str(a) {return a.toString()}
function str
我在前一点写过的还有其他替代方案吗?