简体   繁体   English

什么javascript对象打印为false,是真的,并且是类型“对象”?

[英]What javascript object prints as false, is truthy, and is type “object”?

I'm trying to debug a BIRT report where I've retrieved a paramter from the reportContext like so: var myParameter = reportContext.getParameterValue("myParameter"); 我正在尝试调试BIRT报告,我从reportContext中检索了一个参数,如下所示: var myParameter = reportContext.getParameterValue("myParameter"); BIRT has no debugger and BIRT javascript cannot access JSON.stringify, so I cannot examine what this variable is except by printing it. BIRT没有调试器,BIRT javascript无法访问JSON.stringify,所以除了打印它之外,我无法检查这个变量是什么。 When I print it, it prints as "false". 当我打印它时,它打印为“假”。 typeof(myParameter) is "object", myParameter === null evaluates to false, myParameter === undefined evaluates to false, myParameter == "false" evaluates to false, and myParameter is truthy (if I use it as the guard to an if statement, the if statement is executed). typeof(myParameter)是“object”, myParameter === null计算结果为false, myParameter === undefined计算结果为false, myParameter == "false"计算结果为false,myParameter结果为真(如果我用它作为守护者) if语句,执行if语句)。 What the heck is this variable, and how can I determine what it is? 这个变量究竟是什么,我怎样才能确定它是什么? Is there a way to stringify it without using JSON.stringify, which I cannot access in BIRT? 有没有办法在不使用JSON.stringify的情况下对其进行字符串化,而我无法在BIRT中访问它?

> new Boolean(false).toString()
'false'
> typeof new Boolean(false)
'object'
> !!new Boolean(false)
true
> 

To check whether this is in fact your object, new Boolean(false).constructor returns (stringified) [Function: Boolean] . 要检查这是否实际上是您的对象, new Boolean(false).constructor返回(字符串化) [Function: Boolean]

SLaks给出的答案非常接近,并让我弄清楚它到底是什么:BIRT javascript可以调用Java代码并使用Java对象和类 - myParameter是BIRT的Java布尔对象的Javascript版本(不是Java布尔基元,或Javascript布尔对象,或Javascript布尔基元)。

Consider some custom object with a custom toString() function with the latter returning false. 考虑一些带有自定义toString()函数的自定义对象,后者返回false。

 function MyType() {} MyType.prototype.toString = function() { return "false"; }; var a = new MyType(); console.log( String( a ) ); console.log( typeof a ); console.log( Boolean( a ) ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM