Rendering in vanilla Javascript

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024

Комментарии • 24

  • @AntonisProvidakis
    @AntonisProvidakis 5 лет назад

    *EDIT* : I watched this video: ruclips.net/video/EUYXxsur65U/видео.html of yours, and I see that you set an object containing the function you want to use in the window object.
    ---------------------------------------------------------------------------------------------------------------------------------------
    Hello,
    In new.js example, the use of "otherLogMe" function (as a string) inside the OtherMyLi works just because the otherLogMe function (line 1) is declared in the global scope.
    How is this approach gonna work in an app that uses ES6 modules (webpack, parcel, etc)?
    I mean the console is gonna print "otherLogMe is not defined" or something...

  • @JustAnAverageGuy1985
    @JustAnAverageGuy1985 6 лет назад +3

    Thanks for this, I have also been using the old approach you showed in all of my project. Will be switching to this new method.

    • @FredrikChristenson
      @FredrikChristenson  6 лет назад +2

      It saves quite a bit on boilerplate when I can use a string template so it was a great discovery for me.
      Have a great day and thank you so much for watching!

  • @laylaarju3142
    @laylaarju3142 4 года назад +2

    The old J's file goes above my head. Nice tut sir.

  • @vaibhavarora5067
    @vaibhavarora5067 6 лет назад +2

    Hi Fredrik,
    Thanks for these amazing videos + tips, I find them really useful and thought provoking! :)
    I remember playing around with 3 ways of doing it (some time back) -
    #1. Directly writing to DOM ( appendChild on elements that exist in DOM) : which is what you also do in your first version
    #2. Forming HTML as strings and finally setting innerHTML to reflect it on DOM: which is what you showcased in this video
    #3. Creating elements and appending to a dummy element (that does not exist in DOM but only in memory). eg
    // Example
    var oDummyWrapper = document.createElement("div");
    // add other elements that you're creating to oDummyWrapper ...
    // and then finally fetch the original wrapping element
    var oOriginalWrapper = document.getElementById("element-that-exists-in-DOM");
    // copy the innerHTML over to the element that exists in DOM
    oOriginalWrapper.innerHTML = oDummyWrapper.innerHTML
    #3 is a hybrid approach where you're not manipulating live DOM tree but manipulating a temporary element.
    My JSPerf tests showed that #2 > #3 > #1 (best to worst in terms of speed) .... so the hybrid approach stood second in perf tests...
    It gives you an advantage of adding event listeners easily rather than using attributes inside HTML strings.
    Let me know what you think of it.

    • @FredrikChristenson
      @FredrikChristenson  6 лет назад +2

      Glad to hear that my little hobby is interesting to you!
      This is a great insight m8, I would have sworn the innerHTML copy would lose in terms of performance but then again I have never benchmarked this approach myself.
      What I like about the template string approach is the readability of it all and as long as the performance difference is not that great I would stick with it.
      When it comes to adding event listeners perhaps doing the DOM attachment in 2 waves would be my preference, use method #2 to initialise the element and then query for said element when it has ben added to the DOM to add the event listeners.
      This would perhaps not be great if we had Reacts render approach but then again I would probably build something a bit different if I tried to copy that approach, perhaps I should make a video where we build our own React rendering engine.
      Have a great day and thank you so much for watching!

    • @vaibhavarora5834
      @vaibhavarora5834 6 лет назад

      Thanks mate! :)
      I've used method #2 also in most of the "coding challenge" submissions where I write in comments that I'm using this approach because it's more readable and better on performance :) - always fetches good remarks.
      And a video on our own React rendering engine would be uber cool!

  • @victornpb
    @victornpb 6 лет назад +1

    if you're not already aware of it, you should take a look at polymer.github.io/lit-html/
    P.S. even tho its on the polymer directory, it is its own thing.

    • @FredrikChristenson
      @FredrikChristenson  6 лет назад

      Hi m8!
      Yes I am aware of lit-html, in this video I was just experimenting with string templates as a way to use the standard without any libraries.
      If I where making something a bit more serious I think Polymer is an excellent choice.
      Have a great day and thank you so much for watching!

    • @victornpb
      @victornpb 6 лет назад

      yeah I know, I just thought I would mention lit-html just in case you haven't heard about it.

  • @sumantkanala
    @sumantkanala 6 лет назад

    Move the const text="foo" from 1st way outside and on top of the 1st method. Might be a bit more fair then

  • @davide4607
    @davide4607 4 года назад

    I'm new to programming can you clear up a confusion for me? I read that using innerHTML is bad practice. Is this true or no?

    • @FredrikChristenson
      @FredrikChristenson  4 года назад +1

      There is a slight difference in performance but unless you are making a UI framework like Angular or React it will not matter to you.
      The thing you should never do is to use document.write that has major impact on the application performance.

  • @flywithmendel7711
    @flywithmendel7711 3 года назад

    Using the string method that you are recommending, how would you create a reference to the element for updating?

    • @FredrikChristenson
      @FredrikChristenson  3 года назад

      In a SSR solution you do not create a reference on the server, that is handled on the client.
      The common approach is to use an element id and load some JavaScript that queries for that id but I prefer to use a custom attribute like data-js="myId".

  • @jhwheeler7
    @jhwheeler7 4 года назад

    Wow! This made my day!

  • @trusterzero6399
    @trusterzero6399 4 года назад

    Good stuff!!

  • @mohammedsardar3779
    @mohammedsardar3779 6 лет назад

    hi Fredrik, how is your today ? what vennila js *☺️

    • @FredrikChristenson
      @FredrikChristenson  6 лет назад

      I am doing great Mohammed!
      Vanilla JavaScript is the term I use to describe JavaScript without any framework like React or Angular.
      It is the JavaScript that will run in the browser.
      Have a great day and thank you so much for watching!

    • @mohammedsardar3779
      @mohammedsardar3779 6 лет назад

      Fredrik Christenson Glad to hear that. Oh , js you name like so, yes I too work in Js . But my js pages would have both jquery + js . when I struck at jq will use js to complete the task.

  • @tjalferes
    @tjalferes 6 лет назад

    Thank you for sharing.

    • @FredrikChristenson
      @FredrikChristenson  6 лет назад

      No problem m8, glad you enjoyed it!
      Have a great day and thank you so much for watching!