1

I am working with AbsoluteOrientationSensor

I have an object that contains the values of the accelerometer of the phone.

The values that I need are nested inside an Symbol in the Object.

How do I access those values?

The Object Name is "message".

I have already tried this

console.log(message.__sensor__.quaternion);

But I am getting the result as "undefined".

I have never worked with the Symbol data type in JavaScript before.

The Values that I want to access are the quaternion values

This is the screenshot of the Object Structure -

The Object that I'm working with

Thank you for your help.

1

2 Answers 2

3

From the documentation:

Properties

OrientationSensor.quaternion: Returns a four element Array whose elements contain the components of the unit quaternion representing the device's orientation.

So:

console.log(message.quaternion);

You can also see in the screenshot that the object itself has a getter quaternion.


I have never worked with the Symbol data type in JavaScript before.

Symbols are used for various reasons as property names, but if they are used, it almost always means that you, as a consumer of the API, are not supposed to access that value directly. Instead you should use the "public" API the object provides to access this data, such as in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks for the quick reply. I'm getting four values which are all undefined. Any idea why?
Maybe you are trying to access the data before the sensor has a reading. See developer.mozilla.org/en-US/docs/Web/API/Sensor_APIs#Readings
0

function GetSymbol(object, name) {
  const string = `Symbol(${name})`
  return Object.getOwnPropertySymbols(object).find(symbol => symbol.toString() === string)
}

const __sensor__ = GetSymbol(message, "__sensor__")

console.log(message[__sensor__])

2 Comments

@FelixKling i thought the question was about accessing Symbol property
Kind of, I guess ¯\_(ツ)_/¯ . On a higher level the question is about how to access the data there, and the answer is to use the public API to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.