What do these mysterious double brackets mean in javascript?

Asked

Viewed 328 times

3

Say I raise a Promise to javascript:

var a = new Promise(function(success, error){  { sucess("Sucesso!"); })

My variable a receives the following properties:

Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "Sucesso!"}

However, note that they have double brackets around them. I would like to know how to get the value of properties that have this format.

  • Can give a larger context of how this is being used?

  • @bigown I was doing a dojo that couldn’t change a script and had an array of Promises, wanted a way to identify the Promises before they were solved

  • 1

    @Lfziron my answer solved your question?

  • @Sergio perfectly, thank you

1 answer

3


Those [[ ]] are the manner adopted to represent internal properties of the Precedent visually. A specification of Ecmascript 6 says so:

Internal methods and Internal slots are identified Within this Specification using Names Enclosed in double square Brackets [[ ]].

That is to say:

Internal methods are identified using their names surrounded by square brackets (square parentheses in Pt_pt).

These methods/properties are internal to Promise and may only be used/revealed through .then() or .catch() which are respectively the methods called in case of success and failure of the Promise. So these [[ ]] are not variables with a value but rather methods to be called in case of success or failure, and one of them being called the value of the Promise is revealed, by exclusion of the other unnamed.

  • How bizarre, despite being private the console has access to name and value, but the script does not.

  • @Yes, Lfziron is correct. The console has access because it is a tool for development, but the script has no access because it is an internal property and is not exposed outside the Promise.

  • Is there any way we can create such a property in our objects?

  • @Lfziron "property like that" you mean an internal property that appears that way on the console? Not that I know of.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.