双括号在javascript中意味着什么以及如何访问它们

情况

我有以下使用Promise的功能.

var getDefinitions = function() {
    return new Promise(function(resolve) {
        resolve(ContactManager.request("definition:entities"));
    });
}

var definitions = getDefinitions()

定义的内容是:

Promise {
    [[PromiseStatus]]: "resolved",
    [[PromiseValue]]: child
}

访问PromiseValue属性直接返回undefined

var value = definitions.PromiseValue; // undefined

双括号[[]]的含义是什么,以及如何检索[[PromiseValue]]的值.

解决方法:

什么是[[]]里面的东西

My question is what do the double brackets [[ ]] mean, and how do I retrieve the value of [[PromiseValue]].

这是一个内部财产.您无法直接访问它.本机承诺可能只能通过承诺解包或通常不同步 – 请参阅How to return the response from an asynchronous call.引用规范:

They are defined by this specification purely for expository purposes. An implementation of ECMAScript must behave as if it produced and operated upon internal properties in the manner described here. The names of internal properties are enclosed in double square brackets [[ ]]. When an algorithm uses an internal property of an object and the object does not implement the indicated internal property, a TypeError exception is thrown.

你不能

说真的 – 他们是什么?

非常好!正如上面的引言所说它们只是在规范中使用 – 所以它们没有理由真正出现在你的控制台中.

不要告诉任何人,但这些都是私人符号.它们存在的原因是其他内部方法能够访问[[PromiseValue]].例如,当io.js决定返回promises而不是回调时 – 这些将允许它在保证的情况下快速访问这些属性.他们不暴露在外面.

我可以访问它们吗?

除非您制作自己的Chrome或V8版本.也许在带有访问修饰符的ES7中,现在没有办法,因为它们不是规范的一部分,并且会突破浏览器 – 抱歉.

所以我知道我的价值吗?

getDefinitions().then(function(defs){
    //access them here
});

虽然如果我不得不猜测 – 你是not converting the API correctly to begin with,因为这种转换只会在方法是同步的情况下工作(在这种情况下不返回承诺)或者它会返回一个已经解决的承诺(这意味着你不要根本不需要转换 – 只需返回.

相关文章

最后的控制台返回空数组.控制台在ids.map函数完成之前运行va...
我正在尝试将rxJava与我已经知道的内容联系起来,特别是来自J...
config.jsconstconfig={base_url_api:"https://douban....
我正在阅读MDN中的javascript,并且遇到了这个谈论承诺并且不...
config.jsconstconfig={base_url_api:"https://douban....
这是我的代码main.cpp:#include<string>#include<...