如何获得符号属性的值 [英] How can I get the value of a Symbol property

查看:91
本文介绍了如何获得符号属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NodeJS中有一个对象(准确地说是一个套接字)。



打印时,我看到其中一项是这样的:

  [Symbol(asyncId)]:2781 //数值更改

如何获取此类密钥的值?



我尝试过 socket ['[Symbol(asyncId)]'] ,但未定义



表达式 socket。[Symbol(asyncId)] 显然不起作用。

解决方案

您将无法直接通过键访问它,除非您引用了实际值: Symbol('asyncId'),因为每个符号都是唯一


Symbol()函数返回一个symbol类型的值,具有静态的
属性,该属性公开多个成员的内置对象,具有公开全局符号注册表的静态
方法,并且类似于
内置对象类,但不完整作为构造函数,因为
不支持语法 new Symbol()。


您可以做的是循环通过使用反射来访问对象自身的属性键。 ownKeys ,其中将包含普通属性&符号,然后获取该引用。



您还可以使用: Object.getOwnPropertySymbols()



  function getObject(){//在此范围之外,您无权访问此符号。 const symbol = Symbol('asyncId'); return {foo:‘bar’,[symbol]:42};} const obj = getObject(); console.log(obj); console.log(obj [Symbol(’asyncId')])); // undefined //或Object.getOwnPropertySymbols(obj)const symbolKey = Reflect.ownKeys(obj).find(key => key.toString()==='Symbol(asyncId)')console.log(obj [symbolKey ]); // 42  



注意:对象可以有多个键,其中 key.toString()==='Symbol(asyncId)',这不是很常见,但是请注意,因此您可能想要可以使用 .find 以外的其他功能。



注意II
不应更改该属性的值,因为它应该仅用于内部访问,即使该属性不是只读的。



< pre class = snippet-code-js lang-js prettyprint-override> function getObject(){//在此范围之外,您无权访问此符号。 const symbol = Symbol('asyncId'); const symbol2 = Symbol('asyncId'); return {foo:'bar',[symbol]:'我不需要的值',[symbol2]:'我想要的值'};} const obj = getObject(); const symbolKey = Reflect.ownKeys (obj).find(key => key.toString()==='Symbol(asyncId)')console.log(obj [symbolKey]); //我不需要的值console.log('===所有值==='); Reflect.ownKeys(obj).forEach(key => console.log(obj [key]));


I have an object in NodeJS (a socket to be accurate).

When I print it, I see that one of the entries is this:

[Symbol(asyncId)]: 2781 // the numeric value changes

How can I obtain the value of such key?

I've tried socket['[Symbol(asyncId)]'] but got undefined.

The expression socket.[Symbol(asyncId)] would obviously not work.

解决方案

You will not be able to access it directly by key, unless you have a reference to the actual: Symbol('asyncId'), because every Symbol is unique

The Symbol() function returns a value of type symbol, has static properties that expose several members of built-in objects, has static methods that expose the global symbol registry, and resembles a built-in object class but is incomplete as a constructor because it does not support the syntax "new Symbol()".

What you can do is loop through the object's own property keys, using Reflect.ownKeys, which will include normal properties & symbols, and then obtain that reference.

You can also use: Object.getOwnPropertySymbols()

function getObject() {
   // You don't have access to this symbol, outside of this scope.
  const symbol = Symbol('asyncId');

  return {
    foo: 'bar',
    [symbol]: 42
  };

}

const obj = getObject();

console.log(obj);
console.log(obj[Symbol('asyncId')]); // undefined

// or Object.getOwnPropertySymbols(obj)
const symbolKey = Reflect.ownKeys(obj)
  .find(key => key.toString() === 'Symbol(asyncId)')
  
console.log(obj[symbolKey]); // 42
 

NOTE: The object can have multiple keys where key.toString() === 'Symbol(asyncId)', this won't be usual, but be aware, so you may want to use other function other than .find if that's the case.

NOTE II: You should not change the value of that property, since it's supposed to be for internal access only, even if the property is not read only.

function getObject() {
       // You don't have access to this symbol, outside of this scope.
      const symbol = Symbol('asyncId');
      const symbol2 = Symbol('asyncId');

      return {
        foo: 'bar',
        [symbol]: 'the value I don\'t want',
        [symbol2]: 'the value I want'
      };

}
const obj = getObject();

const symbolKey = Reflect.ownKeys(obj)
  .find(key => key.toString() === 'Symbol(asyncId)')
  
console.log(obj[symbolKey]); // the value I don't want

console.log('=== All values ===');
Reflect.ownKeys(obj)
  .forEach(key => console.log(obj[key]));

这篇关于如何获得符号属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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