微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

双括号在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,因为这种转换只会在方法是同步的情况下工作(在这种情况下不返回承诺)或者它会返回一个已经解决的承诺(这意味着你不要根本不需要转换 – 只需返回.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐