在javascript中查看对象(在引擎盖下) [英] Viewing objects in javascript ( under the hood )

查看:233
本文介绍了在javascript中查看对象(在引擎盖下)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常好奇,如何对象在nodejs中显示,在这种情况下承诺。当使用 console.log(promiseObject)时,输出的类型为

Im very curious as to how objects are displayed in nodejs and in this case promises. When using console.log(promiseObject) the output is of type

{state: pending $

{state:pending}

自从函数 .then()

尝试自己使用此代码

 function a(){

    var deferred = q.defer();

    setTimeout(function(){
        deferred.resolve();
    },4000)

    return deferred.promise;
}

var p1 = a()
console.log(p1) 
//outputs {state:pending} while i was expecting something like
//while i was expecting it to be {state:pending,then:function()}

对我来说感到非常神秘。我也有类似的问题在浏览器中打印对象,似乎有些字段是..隐藏? (虽然我知道在javascript中没有这样的事情)

Feels very arcane to me. I've also had similar problems in printing objects in the browser, seems like some fields are.. hidden? ( though i know of no such thing in javascript )

推荐答案

使用调试器,你的浏览器可能是一个好的。 F12,然后点击下面的运行按钮,即可浏览Promise对象(适用于Chrome / Chromium,Edge和Firefox):

Use a debugger, your browser has probably a good one. F12 in your browser and click the Run button below and you can explore a Promise object (works in Chrome/Chromium, Edge, Firefox):

console.clear();
var a = new Promise(function(res, rej) { res(); });
console.dir(a);

then() catch()和其他函数位于 __ proto __ 属性中。

then(), catch() and other functions are in the __proto__ property.

这篇关于在javascript中查看对象(在引擎盖下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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