Home > Back-end >  Access Symbol(_latestValue) elements of an Observable Array
Access Symbol(_latestValue) elements of an Observable Array

Time:02-24

printerViewModel['printerChecked']["Symbol(_latestValue)"]

I have find value by this code but print undefined from this response, please see this image

enter image description here

I Need false from Symbol(_latestValue) by array elements or object

CodePudding user response:

The public way of getting an observable's value is to run the function. So in this case, you should be able to retrieve the false value by calling:

printerViewModel.printerChecked()

The debugger indicates that this property key is a Symbol. That means you can't access it by using the string "Symbol(...)".

Symbols are usually used to hide stuff from you, so it is supposed to be impossible to retrieve.

The only exception I can think of is if the property was set using Symbol.for. As you can see from this util knockout uses, this isn't the case:

createSymbolOrString: function(identifier) {
  return canUseSymbols ? Symbol(identifier) : identifier;
}

Source: https://github.com/knockout/knockout/blob/2bec689a9a7fcaaed37e6abb9fdec648f86c4f81/src/utils.js#L513


Some basic snippets to help you understand symbols:

const symbolFor = {
  [Symbol.for("_latestValue")]: false
};

const symbol = {
  [Symbol("_latestValue")]: false
};

console.log(
  // This works because the key was created using `Symbol.for`
  symbolFor[Symbol.for("_latestValue")],
  // This does not work
  symbol[Symbol.for("_latestValue")]
);

If you have access to the place that sets this property, you can expose a reference to the symbol:

const secretKey = Symbol("secretKey");

const myObj = {
  [secretKey]: false
};

console.log(
  myObj[secretKey]
)

  • Related