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

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

问题描述

情况

我有以下使用 Promise 的函数.

var getDefinitions = function() {返回新的承诺(功能(解决){解决(ContactManager.request(定义:实体"));});}var 定义 = getDefinitions()

definitions的内容是:

承诺{[[PromiseStatus]]:已解决",[[PromiseValue]]:孩子}

访问PromiseValue 属性直接返回undefined

var value = definition.PromiseValue;//不明确的

问题

双括号[[]] 是什么意思,我如何检索[[PromiseValue]] 的值.

解决方案

[[]]

里面有什么东西<块引用>

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

这是一个内部属性.您无法直接访问它.本机承诺只能在 then 中与承诺一起解包或异步解包 - 参见 如何从异步调用返回响应.引用规范:

<块引用>

本规范定义它们纯粹是为了说明目的.ECMAScript 的实现必须表现得好像它以此处描述的方式生成和操作内部属性.内部属性的名称括在双方括号 [[ ]] 中.当算法使用对象的内部属性并且该对象没有实现指定的内部属性时,会抛出 TypeError 异常.

你不能

说真的 - 它们是什么?

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

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

我可以访问它们吗?

除非您制作自己的 Chrome 或 V8 版本,否则不会.也许在带有访问修饰符的 ES7 中.截至目前,没有办法,因为它们不是规范的一部分并且会跨浏览器中断 - 抱歉.

那么我如何获得我的价值?

getDefinitions().then(function(defs){//在这里访问它们});

但是如果它返回一个错误呢?在这些情况下,在 .then() 的末尾(和外面)添加以下内容.

.catch(function(defs){//在这里访问它们});

虽然如果我不得不猜测 - 你是 没有正确地转换 API 以开始 因为这种转换只有在方法是同步的情况下才有效(在这种情况下不返回承诺)或者它已经返回一个承诺,这将使它得到解决(这意味着您根本不需要转换 - 只需 return.

Situation

I have the following function which uses Promise.

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

var definitions = getDefinitions()

The contents of definitions is:

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

Accessing the PromiseValue property directly returns undefined

var value = definitions.PromiseValue; // undefined

Question

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

解决方案

What's the stuff inside [[]]

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:

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.

You cannot

Seriously though - what are they?

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.

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.

Can I access them?

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.

So how do I get my value?

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

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
});

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天全站免登陆