小编pic*_*cer的帖子

console.log中的%j说明符排除了一些属性

我最近尝试加载bson(二进制JSON)模块node.js.API文档不清楚,所以我认为检查对象会对我有所帮助.检查结果令人费解.

最后我发现这是因为我使用的%j是"谎言" - 它不会打印所有对象的字典键!(我使用Python术语表示"属性"作为用点引用的东西,使用"字典键"表示用括号引用的东西,因为我不知道JS中这些东西的正确名称.)

这是一个例子:

var bson = require("bson");
console.log("bson as %%j: %j", bson);
console.log("bson as console.log: ", bson);
Run Code Online (Sandbox Code Playgroud)

这是输出:

bson as %j: {"BSONPure":{},"BSONNative":{}}
bson as console.log:  { BSONPure: 
   { Code: [Function: Code],
     Symbol: [Function: Symbol],
     BSON: 
      { [Function: BSON]
        BSON_INT32_MAX: 2147483647,
        BSON_INT32_MIN: -2147483648,
        BSON_INT64_MAX: 9223372036854776000,
        BSON_INT64_MIN: -9223372036854776000,
...
Run Code Online (Sandbox Code Playgroud)

我认为既然x.key是相同的x["key"],这意味着属性和字典键在JS中是"相同的东西".我尝试这之后发现BSON.BSONPure{},但BSON.BSONPure.BSONBSON.BSONPure["BSON"]是一个function对象!

这让我相信无论如何"%j",必须以某种方式排除某些键.它如何决定排除哪些键?为什么会这样呢?有时JS是一种非常混乱的语言!

相关Github门票:https: //github.com/mongodb/js-bson/issues/97

javascript properties node.js console.log

12
推荐指数
1
解决办法
6316
查看次数

标签 统计

console.log ×1

javascript ×1

node.js ×1

properties ×1