双括号在javascript中是什么意思,以及如何访问它们 [英] What do double brackets mean in javascript and how to access them

查看:159
本文介绍了双括号在javascript中是什么意思,以及如何访问它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用 Promise 的功能。

I have the following function which uses 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]] 的值。 / p>

Question

What do the double brackets [[ ]] mean, and how do I retrieve the value of [[PromiseValue]].

推荐答案

[[]]

$有什么用b
$ b

What's the stuff inside [[]]


我的问题是括号[[]]是什么意思,以及如何检索[[PromiseValue]]的值。

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

这是内部财产。您不能直接访问它。本地承诺只能与承诺一起在然后中解包,或者通常以异步方式进行解包-请参见如何从异步调用返回响应。引用规范:

It's an internal property. You cannot access it directly. Native promises may only be unwrapped in then with promises or asynchronously in generally - see How to return the response from an asynchronous call. Quoting the specification:


它们仅出于说明目的由本规范定义。 ECMAScript的实现必须表现为好像它以此处描述的方式对内部属性进行了生产和操作。 内部属性的名称括在双方括号[[]] 中。当算法使用对象的内部属性并且该对象未实现指定的内部属性时,将引发TypeError异常。

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.

您不能

很好!正如上面的引文所说,它们只是在规范中使用-因此没有理由让它们真正出现在您的控制台中。

Very nice! As the above quote says they're just used in the spec - so there is no reason for them to really appear in your console.

除了这些,不要告诉任何人确实是私人符号。它们存在的原因是其他内部方法能够访问 [[PromiseValue]] 。例如,当io.js决定返回promise而不接受回调时,这将使它在保证的情况下快速访问这些属性。他们没有暴露在外面。

Don't tell anyone but these are really private symbols. The reason they exist is for other internal methods to be able to access [[PromiseValue]]. For example when io.js decides to return promises instead of taking callbacks - these would allow it to access these properties fast in cases it is guaranteed. They are not exposed to the outside.

除非您自己制作Chrome或V8版本,否则不要这么做。也许在ES7中带有访问修饰符。截至目前,由于它们还不是规范的一部分,将无法通过浏览器中断-抱歉。

Not unless you make your own Chrome or V8 build. Maybe in ES7 with access modifiers. As of right now, there is no way as they are not a part of the specification and will break across browsers - sorry.

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



但是如果返回错误怎么办?为应对这些情况,请在.then()的末尾(外部)添加以下内容。



But what if it returns an error? In prevision towards these cases, add the following at the end (and outside of) of your .then().

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

尽管我不得不猜测-您是无法正确转换API开头,因为这种转换仅在方法为同步(在这种情况下,不返回承诺)或已经返回的承诺将使它得以解决(这意味着您根本不需要转换-只需 return

Although if I had to guess - you're not converting the API correctly to begin with since this conversion would only work in case the method is synchronous (in that case don't return a promise) or it returns a promise already which will make it resolved (which means you don't need the conversion at all - just return.

这篇关于双括号在javascript中是什么意思,以及如何访问它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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