JavaScript Classes vs Prototypes

Поделиться
HTML-код
  • Опубликовано: 26 сен 2024
  • This tutorial discusses the differences in syntax between creating objects in javascript with the class keyword or with the prototype methods.
    Examples are given of doing the exact same things with both versions.
    It is important to understand that the class syntax is being interpreted as using the prototype syntax and that CLASSES do NOT exist in JavaScript.
    Completed Code GIST: gist.github.co...
    Code Note: line 50 should be
    Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype); //extends
    We are setting the prototype of EmployeeP's prototype to link to the prototype of PersonP.

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

  • @judaspriest33715
    @judaspriest33715 4 года назад +96

    I can probably count on one hand how many truly clear and straight forward youtube coding tutorials I've watched, this is one of em, hitting subscribe!

    • @DRocksRecords
      @DRocksRecords 4 года назад +3

      Yeah each time I stumble on a video he made I feel like someone with confident knowledge is showing something useful. I subscribed too

  • @IONYVDFC
    @IONYVDFC 2 года назад +12

    Great video! Being an old-school C++ developer, I personally never understood why JavaScript ever needed to borrow the class syntax. One cannot really deny anymore that classes exists in JavaScript, they surely didn't. I prefer the syntax 'prototype' above 'class'. Many early C++ book editors around 1990 had a hard time explaining what a class is all about, almost apologetic for the confusing word choice. A prototype really says what it does, as it shapes how the instantiated object will look like, while a class has nothing to do with classification.

  • @machineshouldbe
    @machineshouldbe 4 года назад +30

    My head is still spinning, but this breakdown helped me understand how to use classes more. Thank you so much.

  • @rotrose7531
    @rotrose7531 4 года назад +20

    I never dreamed I can understand these class things, but after this tutorial, those abstract things go straight into my head without any resistance. legend teacher, as always thank you.

  • @NourHomsi
    @NourHomsi 2 года назад +1

    I've been a Javascript programmer for more than 10 years, but suddenly, you opened my mind to a whole new sight to OOP in Javascript !!

  • @igoroliveira9146
    @igoroliveira9146 4 года назад +14

    Steve, you are the ONLY ONE I found online, that explained well enough so I could understand, I always tried to find a relationship (a kind of "translation") between functions and classes and you simply explained it. Perfect video. Thank you very much.

    • @LR-bc8js
      @LR-bc8js Год назад

      Same thing here bro :)

  • @Maru-ge6jn
    @Maru-ge6jn 4 года назад +8

    No bs, straight to the point, clear explanations with clear examples. So underrated!

  • @barungh
    @barungh Год назад

    "It is important to understand that the class syntax is being interpreted as using the prototype syntax and that CLASSES do NOT exist in JavaScript."
    Great 👍

  • @sahasrabhujaneuron5975
    @sahasrabhujaneuron5975 5 месяцев назад

    This is exacly what I'm looking for! Now it makes more sense why the syntax in class is constructed in a bit of a weird way. Many thanks!

  • @LR-bc8js
    @LR-bc8js Год назад +1

    I think in 11:16, the line should be Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype) instead.
    Great video! I love it!

  • @Claudia-hz4ly
    @Claudia-hz4ly 3 года назад +1

    Omg! I finally understood prototypes - I have watched three different tutorials and I still did not get that...until I came across this video! Thank you so much!

  • @thippanayugesh8414
    @thippanayugesh8414 4 года назад +10

    this channel is pure gold.

  • @manjoujanzen9375
    @manjoujanzen9375 8 месяцев назад

    great way to explain, clear and in a not too fast speed, so the brain can follow. thank you for the great tutorial.

  • @josiahtobas9172
    @josiahtobas9172 4 года назад +4

    Yooooo...been searching for a clear instructional video on this topic, finally found it. Thanks a lot.

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

    After 10 videos on constructors, classes, and prototypes in javascript, finally a video that makes me really understand....thank you.

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

    such a good tutorial, you don't skip anything and actually understanding what's a class doing and how to simulate it using prototype explains the logic behind it.

  • @abdouthiam7981
    @abdouthiam7981 2 года назад

    This is so useful I cannot explain how much it made my learning prototype easy

  • @glenottley2823
    @glenottley2823 Год назад

    What a clear and insightful video on what can be a very complicated concept! I've saved your 'JavaScript from the start' playlist and will be working my way through, one per day. Thanks Steve for the great education!

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

    Thank you so much. I am not good at english but your explanation is amazing. I can understand whole things. Thank you.

  • @JeatBunkie
    @JeatBunkie 2 года назад

    So happy I found your channel, I could cry! You’re making every subject I was shaky on just make sense!

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

    You are a true engineer, thanks man

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

    I can't stress how clear this was. Thanks

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

    one of the best if not the best javascript channels in youtube

  • @kranthikumar5215
    @kranthikumar5215 Год назад

    I have never seen a explanation like this really u r a great teacher sir applauds 🙏

  • @arinamartens6858
    @arinamartens6858 4 года назад +4

    you are the best! you have nice voice and speech and so good at explaining, thank you

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

    The beeeeeeeeeeest explanation ever.
    Thank you so much mate.

  • @jholtkort
    @jholtkort 5 лет назад +3

    Thank you for creating this. Very good!

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

    Very concise explanation. Better than code bootcamp. Subscribed

  • @federicoprat9093
    @federicoprat9093 2 года назад

    my god finally i understand the difference, Thank you man you saved me.

  • @Mona001-01g
    @Mona001-01g 3 года назад

    You truly deserve a Subscribe. Thank you for breaking down the complexities of a very confusing language!

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

    After making a connection between the EmployeeP object and the prototype of the PersonP using Object.setPrototypeOf() method, a new prototype method(function) is added on the the EmployeeP object (in the line number 51) with the name of employeeInfo. So, my question is that whether this new method (employeeInfo) will be added to the prototype of PersonP or another prototype object of the EmployeeP?
    BTW your way of imparting the information (idea) is unmatchable, it's GOLD STANDARD. I will try to teach my students your way.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад +1

      The new method on line 51 is only being added to the prototype of EmployeeP. With setPrototypeOf on line 50 we are explaining what the next step in the prototype chain will be. If some method is not found inside EmployeeP.prototype then JS will look inside PersonP.prototype.
      This video also helps my students - ruclips.net/video/01jVgCK-HX4/видео.html

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

    Thank you so much, you have helped me sort out the details that were getting mixed up in my head.

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

    Gold! Incredible! Thank you!

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

    As an advanced novice JS coder, I always found 'prototypes' confusing ... Your video makes the case for the class paradigm vice prototype model in my view ... Coming from the Java and Python world, classes and subclasses are the way to go and are more OOP than 'prototypes' ...

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      They both do the same thing internally. Both syntaxes are working with prototypes. The Class syntax was partly added to make the transition easier for people coming from languages like Java. Partly it was yet another attempt at trying to simplify something that has been a confusing mess for 20 years.

  • @sergeyd9989
    @sergeyd9989 2 года назад

    Awesome. You made it clear for me, thank you.

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

    Great tutorial with super clear explanations! Thanks a lot!!

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

    Fantastic explanation. I can always count on this channel to clear things up. Thanks man.

  • @michael.knight
    @michael.knight 5 лет назад +3

    Excellent tutorial, thank you.

  • @wick12298
    @wick12298 Год назад +2

    Its actually Prototype, classes don’t exist in JS. Also, this is “this”. And its also an Object

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

    Yea one of the nice things about class is its inheritance of scope. Thanks

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

    Just discovered your channel, man .. if only you've been around 20 years earlier, I would be a JS master by now :) The thing that confused me for many years is what "this" means depending on the different contexts. I was a AS3 developer for many years, so i was used to the class logic, never understood the "prototype" concept because of that. When "class" was introduced in ES i finally could work the way i was used to. But thats why i've never understood "this". Now, i even think the prototype way of doing things is more logical. And understanding it clears up a ton of other things in JS that puzzled me.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      Glad I could help. I loved teaching AS3 in Flash. Having the visual part made it so much easier to explain the Class concept.
      I did a video recently on "this" - ruclips.net/video/eWDXgsIgTGk/видео.html

  • @chesterxp508
    @chesterxp508 2 года назад

    Another very cool tutorial!

  • @Snoo29293
    @Snoo29293 3 года назад +1

    At 11:13, if you set the prototype of EmployeeP to PersonP.prototype, the EmployeeP.__proto__ becomes the PersonP.prototype, and the EmployeeP.prototype stays the same it was, that's what happens for me at least. Because of that the methods inside PersonP.prototype cannot be called through the objects created by EmployeeP. I don't know if I did something wrong, but to fix that I simply wrote Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype), so now the prototype object of EmployeeP function is a child of the prototype object of PersonP function, I think that's how the prototype chain for classes is too.

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

    Thank you man, you are very talented to explain those concepts... God bless you!

  • @JoseRios-yr6pc
    @JoseRios-yr6pc 3 года назад

    Thanks a lot! It's much clear now the concept to me

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

    wuauu !!! You made it very easy . A lot of thanks, Blessings

  • @B-Billy
    @B-Billy Год назад

    Prime level content!! You are really good at JS❤🎉

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

    Nice content, nice explanation, I'm a newbie & I could derive a lot of sense out of this. Helpful & v.much to the point.
    Clean clear crisp. And your voice is so cool :p
    Cheers keep this great work going.

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

    This is such a good explanation, thanks mate.

  • @gautamjiart
    @gautamjiart 2 года назад

    Really good explanation

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

    I was struggling with 2 things, what a class really is (in JS) & what practical purpose it serves for the work I do.
    You have answered that perfectly .. knowing that the class syntax is just syntactic sugar for functions makes my brain happy!
    Subscribed.

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

    thank you
    it' s a really good one

  • @gayathrisathyan3892
    @gayathrisathyan3892 4 года назад +3

    @steve griffith : Hello Steve. This is really Awesome. By far the best Explanation ever. All your Videos are Awesome. Keep Rocking. A Small Clarification, I feel it has to be Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype). [ You have written EmployeeP. This causes an error and I'm not able to call getDetails Function from the Base Class. ] Please do correct me if I'm wrong. Thanks

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +1

      Yeah. I fixed that in my code sample afterwards. I also have a new video that I did to visually explain prototypes - ruclips.net/video/01jVgCK-HX4/видео.html

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

      Steve Griffith Awesome Man. Thanks

  • @Richard.halabi
    @Richard.halabi 3 года назад

    thanks Steve very helpful.

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

    Thanks it made it clear to me that even if it ends up being the same thing, classes makes it more clear that they are tied together.
    I do wonder if one way performs better than the other tho but its probably not that important.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +1

      There's no performance difference because both syntaxes create the same objects with the same prototypes

    • @alex-desroches
      @alex-desroches 4 года назад +1

      @@SteveGriffith-Prof3ssorSt3v3 nice ok thanks for the reply, have a good day

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

      @@SteveGriffith-Prof3ssorSt3v3 Hi, Steve. Loved the video, thank you. I came here with a burning question that perhaps flew over my head. If they're doing the same thing, then what's the point of the redundant syntax and why chose one syntax over of the other?

    • @lookupverazhou8599
      @lookupverazhou8599 2 года назад

      @@choppinbrixx4931 Trying to make it more declarative. That's what I got out of it anyway.

  • @MannyGonzalezReyna
    @MannyGonzalezReyna 11 месяцев назад

    Thank you professor.

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

    Great video very helpful!

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

    How many times have I watched this video over the last few months? Lost count...

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

    Nice explanation 👍

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

    VERY NICE..... Easy to understand.

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

    Thank you so much, you explained it really well.

  • @hanenezribi4029
    @hanenezribi4029 3 месяца назад

    Grenat tutorial , thanks you

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

    It should be:
    Object.setPrototypeOf(EmployeeP.prototype, PersonP.protoype)
    With code shown in video, you're effectively attaching the employeeInfo() method to PersonP prototype, meaning even PersonP instances would have that method after that point.

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

    thanks for this video man. It really helped a lot.

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

    very clear, thank's

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

    Spiegazione eccellente, complimenti!

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

    Very well explained. I will be hitting subscribe.

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

    very helpful thanks a lot

  • @ShivamMishra-mn6cs
    @ShivamMishra-mn6cs 2 года назад

    best explanation

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

    Thanks, it is very helpfull)

  •  3 года назад

    Great Steve.
    Cristal clear and you make it easy.
    But, what happens if you create a variable without new (i.e: let Jose = PersonP)?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      Without the keyword new, any function returns whatever is written after the first return keyword in that function. If the function has no return statement then it returns undefined.

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

    the feel when "this" is "undefined" :D:D:DD: (line 41)

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

      Wow well nevermind there... cant use arrow functions at that point xD

  • @seanlynch1841
    @seanlynch1841 2 года назад

    This is fantastic. Are there any advantages of one over the other? Maybe private #variables in classes, for example?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 года назад

      It's just two different syntaxes for the same thing.
      Real #private variables are available in classes but you can mimic them with proper scoping and prototypes.

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

    why arent we able to call - console.log(mary.getDetails()) if the getDetails function was passed to function EmployeeP ?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      You can. As long as you changed line 50 to:
      Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype);
      as mentioned in the description. There was a typo in the video.

  • @TraceguyRune
    @TraceguyRune 5 лет назад +1

    is this new? Back in my day, I don't recall JavaScript having classes, much less the ability to extend a class. This is all very Java like.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад +3

      The "class" and "extend" keywords are only a few years old. But remember that they aren't really classes. They are not like classes in Java. JavaScript really only has the "this" keyword because Brendan Eich, when he created the language, was told that he had to include it and make JS "look" like that other popular 90s language "Java". Brendan did not want to use "new" or "this" in JavaScript because it doesn't use classes. Originally JS was going to be called LiveScript. JavaScript was a marketing thing.
      Just remember that it is still only "Java like" on the surface.

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

    Why we don't invoke PersonP function inside PersonP right away instead of using PersonP.call()?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      Because call( ) or apply( ) let us indicate what value to use for this inside the PersonP function, not relying on the lexical scope.

  • @josbellquiros7055
    @josbellquiros7055 2 года назад

    Why do you need line 50? Where you set prototype? It seems that you can have inheritance just with the PersonP.call line. I imagine I'm missing some inheritance functionality that would be missing but I can't think of it.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 года назад +1

      There is a note in the description about a change on line 50. The code was changed in the repo too.

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

    great video man. Thanks

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

    Loved it, pal.

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

    Great explanation!!!

  • @theunknowndev2913
    @theunknowndev2913 Год назад

    Excellent tutorial as usual. I was wondering though, why not add getDetails to PersonP like you did with the properties? I.e. this.getDetails = function() { // code block }. This makes the prototype way look more like the class way. I assume they are just different ways of doing the same thing, is that right?

    • @theunknowndev2913
      @theunknowndev2913 Год назад

      Or there seems to be a difference: the way I mentioned makes 'getDetails' and enumerable property (or method), and using PersonP.prototype.getDetails puts the getDetails method on the PersonP prototype (and anything on the prototype is NOT enumerable). Is that correct?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  Год назад

      @@theunknowndev2913 they are different. Classes use actual inheritance and copying methods and properties to the child objects.
      Prototypes use delegation

    • @theunknowndev2913
      @theunknowndev2913 Год назад

      @@SteveGriffith-Prof3ssorSt3v3 Oh, I see. So would I be correct in saying that classes in ES6 are *not* technically syntactic sugar, in that using classes in ES6 copies methods and properties to the child objects using class-based inheritance, whereas instantiating functions uses delegation (aka prototype-based inheritance)? Assuming that syntactic sugar means an easier syntactic way to do the exact same thing.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  Год назад

      @@theunknowndev2913 they ARE syntactic sugar. They don't copy the properties to the child objects. Delegation means that the properties stay with the single parent object and the reference to the child is used as 'this' when calling on those properties.

    • @theunknowndev2913
      @theunknowndev2913 Год назад

      @@SteveGriffith-Prof3ssorSt3v3 I see. I think I understand. This jibes with the fact that classes are syntactic sugar over prototypical inheritance. I mistook the sugar for the delegation, but the sugar is on top of the delegation. Thanks for your help and great content! I will keep learning :).

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

    Why are methods of a class Person store in Person.prototype and after that we call these methods not passing by prototype ?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      JavaScript automatically walks up the prototype type chain looking for shared methods and properties.
      ruclips.net/video/01jVgCK-HX4/видео.html

  • @KeplerEmeritus
    @KeplerEmeritus Год назад

    Thank you so much for the very concise information. Sometimes I see projects on Github mixing these techniques (Especially Node.js projects). Why is that? Is there some advantage to mixing these or using one or the other?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  Год назад +1

      Classes are just syntactic sugar for prototypes. In the latest updates to classes there are a couple unique things. But for the most part it is a developer preference on how to write the code. Sometimes it will depend on what else they are doing in the code and if it is more functional or more object oriented.

    • @KeplerEmeritus
      @KeplerEmeritus Год назад +1

      @@SteveGriffith-Prof3ssorSt3v3 Understood! Thanks so much.

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

    From what I'm reading, the use of Object.setPrototypeOf() is highly discouraged due to massive performance issues. I also miss how this relates to __proto__ in this very example. Last thing I noticed is that doing SuperClass.call(this, params) is called constructor theft in Nicholas Zakas' excellent book "Principles of object-oriented Javascript", and I wonder if there's any alternatives to that.

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

    Thanks man!

  • @kedarsatwik6938
    @kedarsatwik6938 2 года назад

    What is role of supee(nm,id)

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

    immeditly subscribed

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

    Line no : 50, The setProtoType should be set on EmployeeP's prototype right?
    Object.setPrototypeOf(EmployeeFun.prototype,PersonFun.prototype) // extends

  • @The-cyber-imbiber
    @The-cyber-imbiber 3 года назад

    To me, it feels like Typescript interfaces and type aliases can do all the things that you would use classes/prototypes to do. I feel like i might be wrong about this. Does anybody know anything about this?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад +1

      Typescript is a superset of ECMAScript... which is JavaScript. It just has extra support for things like primitive datatypes that are strongly typed, intefaces, enums, an extra scope level, etc.
      End of the day it still has JS under the hood with prototypes.

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

    Awesome explanation! Now I'm wondering if having the "class" keyword in JS makes things more confusing.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      In some ways it does. But if you understand how the prototype system works then you just need to learn how the class keyword works with this. Just don't try to force your understanding of `class` from other languages into JS.

  • @islombekhasanov
    @islombekhasanov Год назад

    Perfection

  • @anujkumarjha9454
    @anujkumarjha9454 2 года назад

    hi just wanted to know doesn't it breaks linkage with Function.prototype with EmployeeP ?
    I guess instead
    "Object.setPrototypeOf(EmployeeP, PersonP.prototype)"
    we should use
    "Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype)"

  • @Mona001-01g
    @Mona001-01g 3 года назад

    When to use class syntax and when to use prototype syntax?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      It's all prototypes really. So, there is no performance difference. Just a personal choice as long as you understand how the prototypes are created and used.

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

    What happens when you dont put the 'new' keyword there? Im talking about this: let fred = PersonP()

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

    Awesome video! I have a question regarding the number of functions that can be extended. For instance, EmployeeP extends PersoneP1 and PersoneP2.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      You can create a prototype chain that is any length that you need. However, each object's constructor function can only have one prototype. The prototype will be one step in the prototype chain.
      If you are asking about combining the properties and methods of a bunch of different objects into a single new object, then you are talking about composition instead of inheritance. ruclips.net/video/fbpXQ0e8Mp8/видео.html

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

    Can someone explain please? 10:26
    How can he pass 3 arguments (this, nm, id) to a function PersonP, that has only 2 properties (nm, id) ?

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

    Nice tutorial

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

    Hi Steve, thanks for this video. I came back to this again after refactoring some code into factory functions and away from classes.
    My question is: with what you've shown here, will we still encounter the various pitfalls with 'this'? (ie. having to bind explicitly when invoked from another function -- my intuition says yes because this illustrates an alternate syntax for the same end result).
    To that end, would you recommend a more functional / composition based approach where factory functions return objects with certain properties and then object types are returned by composing those functions?
    Cheers!

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +1

      With JS I would definitely recommend using a functional / composition approach to building things. Classes in JS are not what you expect from "class" in other languages. It is a nice clean syntax if you are building objects that will have a limited scope and interactions with other object types. However, when architecting anything in JS you need to think in terms of prototypes and composition works better there.

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

    Thank u so much bro, actually i have a small question which is :: which one should i use, classes or objects, and why ?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      The "class" syntax makes objects with prototypes. They are the SAME thing. It's just different syntax to get the same result.
      It's a matter of preference within the team that you are working on.
      You should know how to use both syntaxes to get the same result.

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

      @@SteveGriffith-Prof3ssorSt3v3
      Thanks bro, you are the best.
      Actually i prefer to use classes, because i think they are a bit clean

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад +1

      @@majdibrahem5586 As long as you understand the objects and prototypes that you create, that's cool.
      Just know that you will encounter code written both ways.

    • @majdibrahem5586
      @majdibrahem5586 3 года назад +1

      @@SteveGriffith-Prof3ssorSt3v3
      Cool, i understand bro. That was really helpful ❤️

  • @PankajDas-mw4uq
    @PankajDas-mw4uq 4 года назад

    By using setprototype method, getDetails method of Person's prototype didn't inherited by EmployeeC. So where is the inheritance now? I wrote the same code in my pc but didn't inherited the method by EmployeeC. Plz explain

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      Did you get type the code that was on the screen or take the updated version from GitHub? If you typed the version on the screen did you read the note in the description about line 50?
      With that change, you can add this to the end:
      console.log(mary.getDetails());
      and it works fine.

    • @PankajDas-mw4uq
      @PankajDas-mw4uq 4 года назад

      @@SteveGriffith-Prof3ssorSt3v3 ohhk sir I got it. Thank u