Object Oriented JavaScript Tutorial #10 - Prototype

Поділитися
Вставка
  • Опубліковано 18 жов 2024
  • Hey gang, in this object oriented JavaScript video, we'll be looking at the prototype object and how we can use it to give our objects methods.
    🐱‍💻 Course Links:
    VS Code editor - code.visualstu...
    GitHub repository (course files) - github.com/iam...
    JavaScript for Beginners - • JavaScript Tutorial Fo...
    🤑 Donate @ www.paypal.me/...
    🎓Find me on Udemy @ www.udemy.com/...
    👾 Video thumbnail artwork by PixelSwish @ / @designable

КОМЕНТАРІ • 144

  • @evanc8057
    @evanc8057 Рік тому +14

    I watched like 6 other videos and read a bunch of articles and still didn't understood why prototypes existed. This was the first video that made sense to me. Thank you!

  • @ademineshat
    @ademineshat 5 років тому +102

    After so many tutorials and online courses I've finally understand the OOJ especially prototypes! Which means, the netninja is the best teacher on UA-cam!
    Thank you very much!

    • @NetNinja
      @NetNinja  5 років тому +11

      Thanks so much :)

    • @friktogurg9242
      @friktogurg9242 6 місяців тому

      @@NetNinja Hi, i would like to ask for your advice. I and my team need to do an assignment that simulates the Scrum framework and follow a BCE model, the goal is to create a simple application (can be a pc, web or mobile application) through an OOP(or OOP-related) language and my team chose javascript. My questions are, would you recommend using Javascript for this and if so, is it better to use classes or prototype for our BCE model requirement?

  • @michaeltrevino4425
    @michaeltrevino4425 4 роки тому +73

    I teach coding and today's subject was the 'this' keyword along with 'new' and how prototypes work and I came here to get a refresher... I am just going to get my student to subscribe to your channel instead lol

  • @topherhicks
    @topherhicks 4 роки тому +8

    I finally understand why prototypes exist and when to use them. I've spend an hour detouring from my Udemy course trying to understand this. Thank you!

  • @whatup6557
    @whatup6557 4 роки тому +6

    Prototypes have been so hard for me to understand and then I watched this video, the concept seemed so easy to grasp. Thank you.

  • @brianking5400
    @brianking5400 2 роки тому +3

    I gotta admit this was very enlightening. I am planning on creating my own course for javascript one day as teaching has always been fulfilling to me. I studied for over a year, then went through a coding boot camp, and came out of it totally lost on using react because I couldn't understand OOP or how classes worked, and this short course totally made it all sensible. I will be saving the whole playlist for reference. So Javascript basically isn't even a class-style OOP language, it's a prototype-style programming language that throws the word class in without actually providing any extra functionality I can think of aside from prettier looking code. I almost fail to see the benefit of adding the class keyword. It actually feels like it's harder to understand for newcomers because you go and try to learn about "class based OOP" when you're actually using prototyping, it's misleading as far as learning goes.

    • @darthvader1368
      @darthvader1368 2 роки тому +1

      EXACTLY MY THOUGHTS. We invest so much time learning Class based JavaScript only in the end to learn that all that class stuff is just syntactic sugar and nothing more. This becomes counter productive if we learn about class stuff first and then learn about prototypes. Because now we are forced to unlearn the way of thought we taught ourselves. We now need to think in terms of prototypes when already have formed a class based thinking process.

  • @brandonmchugh9821
    @brandonmchugh9821 Рік тому +2

    Amazing. Ive been struggling to understand the significance of prototypes, you explained it very clearly!!

  • @jrupp2823
    @jrupp2823 3 роки тому +1

    i am so used to not getting shit but every time you are like "do you understand" im like wtf yes i understand perfectly u r the man

  • @RedEyedJedi
    @RedEyedJedi 5 років тому +74

    Why should we use prototype instead of just including the function within the User function as that also worked? What are the advantages of doing this?

    • @MZ-jb9hi
      @MZ-jb9hi 5 років тому +140

      By using prototype property on the constructor function, Prototype will enable us to easily define methods to all instances of the instances while saving memory. What's great is that the method will be applied to the prototype of the object, so it is only stored in the memory once, because objects coming from the same constructor point to one common prototype object. In addition to that, all instances of userOne or userTwo will have access to that method. Thus, we will be able to save quite a good amount of memory compared to the constructor approach. Apart from low memory usage, the prototype approach is obviously faster in execution when creating new object instances since no time is spent on re-declaring any methods. [Yes, I read this somewhere and copied]. And btw, thanks for asking such an important question, Good day.

    • @RedEyedJedi
      @RedEyedJedi 5 років тому +13

      @@MZ-jb9hi Thank you for answering my question Mohammed. I appreciate it. Have a great day.

    • @jamesdaniels2255
      @jamesdaniels2255 4 роки тому +1

      @@MZ-jb9hi Great work. Thanks for the help

    • @shameekagarwal4872
      @shameekagarwal4872 4 роки тому

      @@jamesdaniels2255 can you please reply to my comment?

    • @jamesdaniels2255
      @jamesdaniels2255 4 роки тому

      @@shameekagarwal4872 which comment?

  • @azeezabass288
    @azeezabass288 11 місяців тому

    This man right here is the best teacher I've ever met on this platform

  • @vaylx2253
    @vaylx2253 3 роки тому +1

    I completed Cold Steele's MASSIVE Web Dev Bootcamp not too long ago, it was an awesome course. Learned a lot. But damn it Shaun, my next course will be with you. You rock!

  • @Nick12_45
    @Nick12_45 6 місяців тому +2

    Thanks! This cleared it up.
    Wait, are you using capslock to capitalize single letters?!

  • @davehunse813
    @davehunse813 4 роки тому +1

    Brilliant work, brilliant explanations. Thank you very much

  • @RsZ789
    @RsZ789 3 роки тому +1

    Great job explaining prototypes. So easy so simple. Fantastic work

  • @simpleprogramming4671
    @simpleprogramming4671 2 роки тому

    I just love the way you explain .... so easy to understand!

    • @NetNinja
      @NetNinja  2 роки тому +1

      Glad you think so :) thanks for watching!

  • @weinnandhasanion
    @weinnandhasanion 4 роки тому

    Best tutorial channel ever on UA-cam! Thank you so much!

  • @boggyice
    @boggyice 6 років тому +3

    Wow, this is awesome! Thanks Ninja

  • @watchyoutb
    @watchyoutb 4 роки тому +1

    filled in a few gaps in knowledge for me, thanks!

  • @gururajmoger8649
    @gururajmoger8649 6 років тому +15

    Please do more on javascript programmings..

  • @mahmoudsalah7628
    @mahmoudsalah7628 4 роки тому

    I have struggled with this for quite a while ,...thanks a lot...!

  • @Ahmed-El-Esseily
    @Ahmed-El-Esseily 27 днів тому

    Pretty clear! Thank you very much!

    • @NetNinja
      @NetNinja  27 днів тому

      Thanks for watching Ahmed!

  • @tymothylim6550
    @tymothylim6550 3 роки тому +1

    Thank you very much for this video :) It was very clear and educational!

  • @ohiwantyoutobelieve
    @ohiwantyoutobelieve 2 роки тому

    Excellent! ty so much Ninja

  • @imBROWN23
    @imBROWN23 2 роки тому

    Really helpful for understanding PROTOTYPES in best practical way 👍

  • @Anton-kn2yn
    @Anton-kn2yn Рік тому

    wow amazing video thank you lots of help compared to the other videos out there

  • @ProSimples
    @ProSimples 4 роки тому

    Brilliant. Absolute beautiful. Thanks a million.

  • @BilalRehan-bl6bx
    @BilalRehan-bl6bx 5 років тому +1

    very nice explanation.
    Thanks Ninja

  • @Rolfikv
    @Rolfikv 3 роки тому

    you are my new guru man. thank you so much!

  • @maurov2104
    @maurov2104 4 роки тому +3

    Great video, man!
    One question:
    Why can't we use arrow functions when defining methods using the prototype property?

    • @grasplearning4597
      @grasplearning4597 4 роки тому +4

      because if we use arrow function then this keyword will refer to the global window object and not the empty object that is created by the new object (hope this helps)

    • @maurov2104
      @maurov2104 4 роки тому +1

      @@grasplearning4597 Thanks, Pankaj

  • @raymondmichael4987
    @raymondmichael4987 6 років тому

    Two simultaneously notification ~ ninja videos.
    Thanks bro

  • @1stinfamous
    @1stinfamous 5 років тому

    Thanks mate for your explanations from France 🇫🇷 !

  • @centaur26
    @centaur26 3 роки тому

    this was beautifully explained, thank you much for this.

  • @davidmurph3048
    @davidmurph3048 3 роки тому

    thank you so much for this clear explanation so helpful

  • @sarvarsharapov3004
    @sarvarsharapov3004 6 років тому

    very precise and understandable explanations. Thank you

  • @rayaqin
    @rayaqin 3 роки тому

    Great video man.
    The one thing about this series that bugs me is the LOCK icon flashing on screen every time you toggle Shift (or caps?). Maybe next time try to not have that?

  • @Thud94
    @Thud94 4 роки тому

    I really liked your teaching way and the video! Thanks

  • @volatilityclustering5706
    @volatilityclustering5706 4 роки тому

    Thanks for your easy-to-understand explanation. One question though: function User( ) defines a function which takes two arguments email and name. Why does it not return an object?

  • @myelibrary
    @myelibrary 7 місяців тому

    Brilliant explanation

  • @sikkco
    @sikkco 2 роки тому

    My friend this was a beautiful explanation and it helped me so much!

  • @faiqababar7600
    @faiqababar7600 3 роки тому

    Good explanation. Thanks 👍

  • @RNeilen216
    @RNeilen216 3 роки тому +1

    Starting @ 4:48, you began to show how to access the prototype and, thereafter, I tried following along. However, in the console, I get "undefined has logged on".

  • @svetlana_wo
    @svetlana_wo 3 роки тому

    You explain well! Thanks

  • @dim4atij
    @dim4atij 3 роки тому

    Is it fair to say that adding methods to the prototype of a constructor instead of directly on the constructor also has an advantage of more efficient memory use (that method only stored once and referred through the prototype chain, instead of being created in every instance)? If so - can you add that on the video as well? Thank you - this playlist has been so helpful!

  • @felixt.5409
    @felixt.5409 6 років тому +1

    Great tutorial, thank you very much!
    I have one question: why don't you create an object for the User.prototype like User.prototype = { login(){...}, logout(){...}} ?

  • @andrewdalton6973
    @andrewdalton6973 2 роки тому

    I know this was a while ago, but I was wondering if this was accurate. Basically we're storing methods inside of the proto because if we have a bunch of objects we don't want to have the same method on all of them. This would take up a lot of memory. This is why we store the methods inside the proto property, using the prototype.

  • @MK-vu4qt
    @MK-vu4qt 6 місяців тому

    Well explanation thank you!

    • @NetNinja
      @NetNinja  6 місяців тому

      Glad it was helpful! :)

  • @devmclovin
    @devmclovin 3 роки тому

    Excellent explanation

  • @sadboy1022000
    @sadboy1022000 5 років тому

    very clearly ! thank you !

  • @fredpourlesintimes
    @fredpourlesintimes 3 роки тому

    I noticed that you didn't talk about "super()" and static methods.
    Also, you didn't talk about the fact that, by default, the constructor of a class is by default object (maybe I'm wrong, but I'm pretty sure having seeing that).
    For the rest, you pretty well explain what we need to know about the real structure of class.
    Thanks.

  • @konstantinkotenko270
    @konstantinkotenko270 3 роки тому

    Great lessons, but just would like to address a little concern. In previous tutorials it was easy to follow your explanation when I could always see your mouse cursor.
    In this tutorial series cursor is invisible when you are in code editor and that makes it harder to follow your lecture.
    Ps. Not trying to be picky.... great lessons overall. (But the cursor is very important)

  • @selvaganeshm1914
    @selvaganeshm1914 2 роки тому

    I hope you know you mean a lot to us❤
    Thank you for everything...

    • @NetNinja
      @NetNinja  2 роки тому

      Thank you for your support Selva :)

  • @moamenzakaria4771
    @moamenzakaria4771 4 роки тому

    Thanks a lot nice explanation, it's clear for me now :D

  • @0the0ambient0
    @0the0ambient0 5 років тому

    Excellent description

  • @mognomoinak1309
    @mognomoinak1309 3 місяці тому

    very well explained

    • @NetNinja
      @NetNinja  3 місяці тому

      Thanks, that means a lot :)

  • @Nate-nr8cc
    @Nate-nr8cc 4 роки тому

    Thank you this was so helpful

  • @noy5626
    @noy5626 Рік тому

    Very helpful! 🔥

  • @Trickolo
    @Trickolo 3 роки тому

    Basically prototype functions are emulated static class functions?

  • @KraasRas
    @KraasRas 4 роки тому

    Please do a video about getter and setter

  • @badselection94
    @badselection94 4 роки тому

    Great vid! I’m just confused about one thing. In the video on methods, writing a method inside a class put that method on said classes __proto__ object. Does this mean that attaching methods directly onto a prototype should only be done in the case of a constructor function like in this vid?

  • @nibiay3985
    @nibiay3985 2 місяці тому

    I see. So when you run a login function on an instance of an object, that instance doesn't contain the login function but there is a reference to it. So when you run that function, it goes looks to the original object's prototype chain to find that function and then runs it. I guess this saves memory?

  • @AE-yr6mo
    @AE-yr6mo 3 роки тому

    Do you recommend using the Class keyword or this method?

  • @tricktreat7941
    @tricktreat7941 2 роки тому

    Brilliant!

    • @NetNinja
      @NetNinja  2 роки тому

      Thanks for watching!

  • @christianmoller-hansen7752
    @christianmoller-hansen7752 2 роки тому

    Maybe a dump question, but things may have changed over time.
    How do you export the proto functions in a modul type?
    I tried almost everything

  • @johnconnor9787
    @johnconnor9787 3 роки тому

    Awesome!!!

  • @yvng4697
    @yvng4697 2 роки тому

    tysm mate!

  • @davideugene7911
    @davideugene7911 6 років тому

    Really excellent!

  • @maurov2104
    @maurov2104 4 роки тому +1

    Prototype methods == "static" methods (static like in Java)?

  • @chicoselfie1602
    @chicoselfie1602 3 роки тому

    You are the best!

  • @muhammadumarsotvoldiev8768
    @muhammadumarsotvoldiev8768 Рік тому

    than you very much

  • @allassanedicko1542
    @allassanedicko1542 Рік тому

    Thank you

  • @deeme3433
    @deeme3433 3 роки тому

    Awesome

  • @A.I.Office
    @A.I.Office Рік тому

    when should i use prototype instead of a class?

  • @carpediem73
    @carpediem73 4 роки тому

    thank you

  • @abhineet3695
    @abhineet3695 6 років тому +3

    Better way to define custom prototype:
    User.prototype = {
    login(){
    this.online = true;
    console.log(this.email, 'has logged in');
    },
    logout(){
    this.online = false;
    console.log(this.email, 'has logged out');
    }
    }

    • @kamikaze9785
      @kamikaze9785 3 роки тому

      But why is it better than the other this one seems complex imo

  • @davideugene7911
    @davideugene7911 6 років тому

    Salut le Monde :)
    Merci beaucoup ;)

  • @paudelamarianaoancea3761
    @paudelamarianaoancea3761 4 роки тому

    The best !!

  • @benrogers5736
    @benrogers5736 4 роки тому

    Excellent explanation! Thank you!

  • @chinmayghule8272
    @chinmayghule8272 3 роки тому

    You didn't explain the most important parts: What is the difference between __proto__ and prototype? What is the so-called 'prototype chain'?

  • @l3aIIin23
    @l3aIIin23 4 роки тому

    thanks

  • @ericalexander3816
    @ericalexander3816 4 роки тому

    Wait why you still have this.online = false; in the Function User when you already copied it to the User.prototype? is that suppose to be there still?

  • @DK-df2hi
    @DK-df2hi Рік тому

    Is the prototype method static and that is why it is written like that?

  • @lserranoit
    @lserranoit 2 роки тому

    Even a variable has a prototype. All in JS is an Object with a type.

  • @BrawlhallaClips-nf3nx
    @BrawlhallaClips-nf3nx 4 роки тому +1

    What theme r u using

  • @Tuttigiu
    @Tuttigiu 3 роки тому

    my Atom editor won't accept prototype.login and when typing name.login( ) will return "Login is not a function!"
    but if I choose instead prototype.logon or prototype.logwhatever it goes just fine. Meh

  • @hachij_
    @hachij_ 6 років тому +3

    Why we cannot use 'arrow function' on creating prototype? anyone?
    i try to use like this but, but seem like they don't recognize 'this' keyword? (return undefined)
    User.prototype.login = () => {
    this.online = true;
    console.log(this.email, 'has logged in');
    }

    • @JurajPecháč
      @JurajPecháč 6 років тому

      Arrow function provide a lexical this.

    • @hachij_
      @hachij_ 6 років тому

      Juraj Pecháč what?? i didn't quite understand, can you elaborate more on that

    • @JurajPecháč
      @JurajPecháč 6 років тому

      dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

    • @hachij_
      @hachij_ 6 років тому

      Juraj Pecháč thanks for that. i see that 'this' are still refer to 'window' object when using 'arrow function', that why i get 'undefined'

  • @ravi_sorathiya
    @ravi_sorathiya 4 роки тому

    I think its like static method I guess?

  • @amirkaargar
    @amirkaargar 2 роки тому

    bro whats your theme?

  • @amirjiryan114
    @amirjiryan114 5 місяців тому

    hey guys . dont modify the prototype. that is built for built-in javascript features not you

  • @teacul
    @teacul 3 роки тому

    why does your __proto__ have a __proto__?

  • @tomasmatusek6292
    @tomasmatusek6292 Рік тому

    Can someone help me please understand something ?
    I understud inheritance,classes/constructors.
    But whith I have problem is prototype and the functions. I understud that prototype is some kind of methodology what we can create and append, ok and we create the method outside function of User object. And same for not always repeat the method in our object User. But What I not understand is the function and class/constructor difference. Its like that we can not use prototype for class User ,for that reason we use function User ( .., .. ){ } ? We can use only prototype method for function object ? And if I'm right can someone please help me and show me some example where We can use class User and we have inside made greeting() method, and after outside we will want create prototype on class User like User, prototype.logout ... If its even possible?
    Thank your guys (ninjas )

  • @BachonKiDuniya307
    @BachonKiDuniya307 2 роки тому

    What is "instances "?

  • @ilasilas3261
    @ilasilas3261 3 роки тому

    Why does prototype have it's own prototype?

  • @santiagopabloortiz6322
    @santiagopabloortiz6322 2 роки тому

    Gold

  • @faiqkhan7545
    @faiqkhan7545 3 роки тому +1

    is [[prototype]] = __proto__ ??? someone pls ans this

  • @DKolha
    @DKolha 5 років тому

    could we put:
    user.__proto__.login = function(){...}
    ?

    • @MegORacer
      @MegORacer 5 років тому

      Yes, technically you can, but as far as I can tell, accessing the __ and _ methods and properties is a kind of tabu among the programmers.
      And the __proto__ itself became a part of the ECMA standard in about ES6 or maybe a bit earlier, so before it was just like console.log(.error) - not standartised.

  • @RNeilen216
    @RNeilen216 3 роки тому

    Also I forgot to mention in my console log, it does not show that there is a __proto__ property

  • @paristar3079
    @paristar3079 5 років тому

    #programming

  • @cryptowolf9753
    @cryptowolf9753 2 роки тому

    Prototype 'propty'

  • @kalamashaka8280
    @kalamashaka8280 6 років тому

    Hi i have finished your OOP please if you know of any start ups that can use a rockie i would like to join. I am Nelson From nairobi Kenya

  • @BobbyBundlez
    @BobbyBundlez 2 роки тому

    sitll so confused about the countless ways to just make a frickin object. here you just havea function... no constrcutor keyword. then what about constructor functions? all so confusing

    • @BobbyBundlez
      @BobbyBundlez 2 роки тому

      does this example use constructor functions?