JavaScript Symbols - HTTP203

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 มี.ค. 2018
  • Jake Archibald explains JavaScript Symbols to Surma in 2 minutes. What do you use them for!? Why!? All will be revealed... #HTTP203
    Subscribe to Chrome Developers here: goo.gl/LLLNvf
    Watch more HTTP203: goo.gl/bTQMrY
    Listen to the HTTP203 podcast for a full hour conversation & more: goo.gl/LR7gNg
    Itunes: goo.gl/cf2yRq
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @noidar
    @noidar 6 ปีที่แล้ว +27

    It will be great if you guys will show a "real world" implementation examples of what you're explaining!

  • @LGND-cm5ro
    @LGND-cm5ro 6 ปีที่แล้ว +46

    As a newer developer, I feel that these type of videos would be way more useful with live code examples, rather than just strict conversation. I realize that the point may be to have your audience go and research on their own, but in the moment being able to see an example would be more helpful in my opinion.

    • @jakearchibald
      @jakearchibald 6 ปีที่แล้ว +8

      You're not the first to say this, and it's something we're going to try at the next recording.

    • @zyst6
      @zyst6 6 ปีที่แล้ว +5

      I dunno I really enjoy this format. It's kind of like an appetizer that either fails to interest me, or makes me curious enough to go research the topic on my own.
      I think the 2 minute format might not necessarily lend itself too well to spend too much time doing code demos and the like. Still, if they do adopt code examples I'd be curious as to how they'd pull that off.

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

      I'm interested to see how we pull it off too! We'll still do the podcast, which doesn't rely on visuals.

    • @neovov
      @neovov 6 ปีที่แล้ว

      Well, as a non-english speaking creature, I find it more difficult to grasp all the subtleties of the topics you might talk about. This is even worse with the podcast ; that's why a video with examples would be better and would provide concrete use cases.
      This video is a good example, because I never know when to use Symbols (I tried in a class to hide properties but it was useless since we can ask an object to list its Symbols). The only use case I can see if I want to manually define an Iterator.
      Thanks for the good work though, it's always a pleasure to see you both chatting.

  • @squirrelmanish
    @squirrelmanish 6 ปีที่แล้ว +9

    JavaScript Symbols! What do you use them for? Do you use them for anything? Let's find out!

    • @nahueljo
      @nahueljo 6 ปีที่แล้ว

      Enrique M. Mr peanutbutter?

  • @praveenX
    @praveenX 6 ปีที่แล้ว +4

    Guys, you are doing great and thanks for uploading these videos. if possible, could you pls add few code examples with the dicussion.

    • @dassurma
      @dassurma 6 ปีที่แล้ว

      We are gonna attempt to solve this the next time we record :)

  • @fabiocroldan
    @fabiocroldan 6 ปีที่แล้ว

    I'm a super fan of #HTTP203. You are my favorite youtubers. 👍

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

    Anyone have more information regarding Symbols across s?

    • @MachineYearning
      @MachineYearning 6 ปีที่แล้ว +3

      Here's a link to the blog post I think they talked about: jakearchibald.com/2017/arrays-symbols-realms/
      I found it in the episode notes for the full podcast here:
      developers.google.com/web/shows/http203/podcast/

    • @qabandiman
      @qabandiman 6 ปีที่แล้ว

      TY!

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

    i didnt know about this -symbol-for-sharing-thing... interesting!

  • @MaxArt2501
    @MaxArt2501 6 ปีที่แล้ว

    I've always felt that Symbols were most useful with "native" (well-known) ones, like Symbol.iterator as you mentioned.
    For the rest, I still have to find a solid use case.
    Non-enumerable properties? Object.defineProperty got us covered.
    Private properties? Not really, since we have Object.getOwnPropertySymbols and Reflect.ownKeys.
    Moreover, symbols aren't really polyfillable, so you can't use them if you have to support IE.
    Does anyone have any experience with symbols outside the well-known ones?

  • @Era25601
    @Era25601 6 ปีที่แล้ว

    I'd like to watch something about proxies soon!

    • @dassurma
      @dassurma 6 ปีที่แล้ว

      Noted!

  • @sbruchmann
    @sbruchmann 6 ปีที่แล้ว +2

    203? That's Numberwang!

    • @MartinHodgkins
      @MartinHodgkins 6 ปีที่แล้ว

      Steffen Bruchmann www.google.co.uk/search?q=numberwang

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

    More on symbols please!

    • @hobbyturystaSEO
      @hobbyturystaSEO 4 ปีที่แล้ว

      2.years gone since this. Video has been uploaded and no more symbols has been seen

  • @MartinHodgkins
    @MartinHodgkins 6 ปีที่แล้ว

    So it's all going to change soon. What a surprise.

    • @jakearchibald
      @jakearchibald 6 ปีที่แล้ว

      I don't think it's going to change.

  • @fnvtyjkusg
    @fnvtyjkusg 6 ปีที่แล้ว

    Like your glasses!

  • @qwmf05gcpt42
    @qwmf05gcpt42 6 ปีที่แล้ว

    What about dart?

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

      No.

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

    I would like a medium Fries and a double Cheese Burger. Thank you

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

      Royale with cheese?

  • @MauroGabrielTitimoli
    @MauroGabrielTitimoli 6 ปีที่แล้ว

    Symbols are hidden not because they are not enumerable, but since from es2015 keys now are divided into 2 categories:
    1. names
    2. symbols
    So all the methods that already existed by then only take into account names. There is a big problem with nomenclature, since some old method names use "key" or "keys" but it's no longer correct.
    So, let's take a look from the spec to Object.keys (www.ecma-international.org/ecma-262/7.0/#sec-object.keys):
    > When the keys function is called with argument O, the following steps are taken:
    >
    > 1. Let obj be ? ToObject(O).
    > 2. Let nameList be ? EnumerableOwnNames(obj).
    > 3. Return CreateArrayFromList(nameList).
    >
    > If an implementation defines a specific order of enumeration for the for-in statement, the same order must be used for the elements of the array returned in step 3.
    So, what it's taken into account is the result of EnumerableOwnNames (www.ecma-international.org/ecma-262/7.0/#sec-enumerableownnames), let's have a look to it:
    > When the abstract operation EnumerableOwnNames is called with Object O, the following steps are taken:
    >
    > 1. Assert: Type(O) is Object.
    > 2. Let ownKeys be ? O.[[OwnPropertyKeys]]().
    > 3. Let names be a new empty List.
    > 4. Repeat, for each element key of ownKeys in List order
    > a. If Type(key) is String, then
    > i. Let desc be ? O.[[GetOwnProperty]](key).
    > ii. If desc is not undefined, then
    > 1. If desc.[[Enumerable]] is true, append key to names.
    > 5. Order the elements of names so they are in the same relative order as would be produced by the Iterator that would be returned if the EnumerateObjectProperties internal method was invoked with O.
    > 6. Return names.
    So as we can see here, there one main concept: keys, which they can be symbols, or names (strings), and Object.keys, and a lot of methods from es5 and below use EnumerableOwnNames algorithm to get the names (not well named "keys").
    TL;DR
    No matter that both are keys, symbols shouldn't be confused with names, since these last ones are strings, so we have to check from the spec the method that we would like to use to see what considers to be a key, if it's talking about names (strings) or symbols.

    • @jakearchibald
      @jakearchibald 6 ปีที่แล้ว

      Yep. That's why they're useful for iterator callbacks, rather than using a name like toJSON. But they aren't hidden due to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols

    • @MauroGabrielTitimoli
      @MauroGabrielTitimoli 6 ปีที่แล้ว

      Jake Archibald I just wanted to comment that they aren't hidden because they are not enumerable, but because they aren't taken into account by old methods and new ones that were skipped on purpose. The thing is that on the video you agree that the hiddeness reason is enumerability, and I believe this could confuse some people...

    • @jakearchibald
      @jakearchibald 6 ปีที่แล้ว

      I say "it's kinda hidden *and* it's not enumerable", but I suppose it's possible to mistake the "and" for "because".

    • @MauroGabrielTitimoli
      @MauroGabrielTitimoli 6 ปีที่แล้ว

      I know that it's impossible to explain in depth any concept in that short time, and your videos are amazing, but in this particular case I believe enumerability shouldn't have ever mentioned, since it doesn't have to do with symbols hiddeness, and IMHO in the video this is the reason you agree is promoting that

    • @jakearchibald
      @jakearchibald 6 ปีที่แล้ว

      My intent wasn't to promote hiddenness. The benefit is things like Symbol.iterator & co, vs why obj.toJSON isn't great.

  • @platin2148
    @platin2148 6 ปีที่แล้ว

    So a symbol is a singleton.

    • @noidar
      @noidar 6 ปีที่แล้ว

      no

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

    It always felt like a worse supported weakmap to me.

    • @dassurma
      @dassurma 6 ปีที่แล้ว

      I can’t quite follow. WeakMaps are something completely independent of symbols.

    • @Pesthuf
      @Pesthuf 6 ปีที่แล้ว

      In the end, they allow you to do pretty much the same thing, don't they?Adding extra information to an object that you can only access when you have access to the weakmap / symbol that was used.
      Aside from the built in well-known symbols, which you can't replace with WeakMap, I don't really see a difference between them except obviously their Syntax.

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

      Pesthuf You can always get the reference to a symbol with Objet.getOwnPropertySymbols, so no, WeakMaps are a totally different thing.
      I guess most of the value of symbols is exposing some of the internals of JavaScript to developers, so they can "hack" their way with operators and methods like instanceof, for...of, concat() and so on.

    • @Pesthuf
      @Pesthuf 6 ปีที่แล้ว

      Thanks, I didn't know about Object.getOwnPropertySymbols at all. I had always assumed (and I think even read) that because Symbols are unique, they are also secret (unless it's exposed with Symbol.for or a module).
      That's pretty neat,. then. Guess they are actually pretty useful and unique.

  • @AceixSmart
    @AceixSmart 6 ปีที่แล้ว +3

    HTTP203 videos are now boring 😪

    • @dassurma
      @dassurma 6 ปีที่แล้ว

      What makes you say that? I am interested in your feedback.

    • @AceixSmart
      @AceixSmart 6 ปีที่แล้ว +2

      the interruptions are too many so i dont really get the main point clearly
      ...and everyone wants to dance to the alarm..lol

    • @dassurma
      @dassurma 6 ปีที่แล้ว +2

      I hear you with the interruptions. I have noticed that myself and it’s something I wanna work on in the next recording :D I hope that will solve your second point as well. The dancing will continue tho. In fact, it’ll get worse. Probably.