symbol datatype in javascript | consfused when and where to use symbol?

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.พ. 2022
  • A “symbol” represents a unique identifier.
    A value of this type can be created using Symbol():
    Symbols allow us to create “hidden” properties of an object, that no other part of code can accidentally access or overwrite.
    Symbols are skipped by for…in
    Symbol is used when:
    “Hidden” object properties. If we want to add a property into an object that “belongs” to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in for..in, so it won’t be accidentally processed together with other properties. Also it won’t be accessed directly, because another script does not have our symbol. So the property will be protected from accidental use or overwrite.

ความคิดเห็น • 6

  • @aheteshamshaikh007
    @aheteshamshaikh007 หลายเดือนก่อน

    Great video, thanks for clearing the concept.

  • @dankierson
    @dankierson 2 ปีที่แล้ว

    But you can use other means to find hidden keys. So Symbol wasn't devised for that purpose.

    • @quickcodeacademy
      @quickcodeacademy  2 ปีที่แล้ว

      yes I agree with you, but even if we find hidden keys, we cannot perform any operation on hidden key and there value.

    • @codeforbeer
      @codeforbeer ปีที่แล้ว +1

      But the same thing we can achieve through the enumerable attribute?

    • @_aurora60
      @_aurora60 11 หลายเดือนก่อน

      @@quickcodeacademywe can, using a random Object function which gives you all the symbols, I think it is Object.getOwnSymbols(obj), and after that you can use the symbols to get the values :)