JavaScript GETTERS & SETTERS are awesome!!! 📐

Поделиться
HTML-код
  • Опубликовано: 3 июл 2024
  • // getter = special method that makes a property readable
    // setter = special method that makes a property writeable
    // validate and modify a value when reading/writing a property
    00:00:00 intro
    00:01:42 example 1 setters
    00:04:10 example 1 getters
    00:07:16 example 2
    00:08:31 example 2 setters
    00:11:15 example 2 getters
    00:12:48 conclusion

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

  • @BroCodez
    @BroCodez  7 месяцев назад +6

    // ---------- EXAMPLE 1 ----------
    class Rectangle{
    constructor(width, height){
    this.width = width;
    this.height = height;
    }
    set width(newWidth){
    if(newWidth > 0){
    this._width = newWidth;
    }
    else{
    console.error("Width must be a positive number");
    }
    }
    set height(newHeight){
    if(newHeight > 0){
    this._height = newHeight;
    }
    else{
    console.error("Height must be a positive number");
    }
    }
    get width(){
    return `${this._width.toFixed(1)}cm`;
    }

    get height(){
    return `${this._height.toFixed(1)}cm`;
    }
    get area(){
    return `${(this._width * this._height).toFixed(1)}cm`;
    }
    }
    const rectangle = new Rectangle(2, 3);
    console.log(rectangle.width);
    console.log(rectangle.height);
    console.log(rectangle.area);
    // ---------- EXAMPLE 2 ----------
    class Person{
    constructor(firstName, lastName, age){
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    }
    set firstName(newFirstName){
    if(typeof newFirstName === "string" && newFirstName.length > 0){
    this._firstName = newFirstName;
    }
    else{
    console.error("First name must be a non-empty string");
    }
    }
    set lastName(newLastName){
    if(typeof newLastName === "string" && newLastName.length > 0){
    this._lastName = newLastName;
    }
    else{
    console.error("Last name must be a non-empty string");
    }
    }
    set age(newAge){
    if(typeof newAge === "number" && newAge >= 0){
    this._age = newAge;
    }
    else{
    console.error("Age must be a non-negative number");
    }
    }

    get firstName(){
    return this._firstName;
    }
    get lastName(){
    return this._lastName;
    }
    get fullName(){
    return this._firstName + " " + this._lastName;
    }
    get age(){
    return this._age;
    }
    }
    const person = new Person("Spongebob", "Squarepants", 30);
    console.log(person.firstName);
    console.log(person.lastName);
    console.log(person.fullName);
    console.log(person.age);

    • @Loki_chan
      @Loki_chan 2 месяца назад

      txs bro for these lessons

  • @Sernik_z_rodzynkamii
    @Sernik_z_rodzynkamii 5 месяцев назад +13

    What a great explanation! I am doing the Codecademy course recently and their examples of getters and setters weren't that clear to me.

  • @antcannon
    @antcannon 4 месяца назад +2

    420 69’s parents was living a wild life. The birth certificate did not have any setters and getters.
    Great video. This is going to help me tackle more coding challenges and create cleaner code.

  • @vanta6lack
    @vanta6lack 2 месяца назад +1

    As said by previous commentators, this is literally the best explanation that I've come across so far and Getters and Setters do seem to be quite extraordinary. Huge thank you!

  • @yunusdurdygulyyew9270
    @yunusdurdygulyyew9270 4 месяца назад +2

    That's the best explanation that I could find online. Thank you for the work you do!

  • @shafiulAlamShafi
    @shafiulAlamShafi 7 месяцев назад +1

    Thanks for explaining!!

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

    best explanation i've ever seen here on youtube.
    thank u so much bro!!!

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

    this playlist has been soooo helpful! thank you for the amazing free content. hugs from brazil ✨

  • @tonytodd7011
    @tonytodd7011 7 месяцев назад +1

    Great explanation, thanks Bro!

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

    You are live saver bro ❤

  • @khalidmadaraobito8651
    @khalidmadaraobito8651 7 месяцев назад +1

    Perfect💥

  • @user-qv2sh6wm6j
    @user-qv2sh6wm6j 6 месяцев назад +1

    Thanks Bro!!

  • @hunin27
    @hunin27 7 месяцев назад +1

    thanks bro. would you mind doing a javascript project with all the things we learned? At least i could try it myself as well before seeing how you do it

  • @baxti7
    @baxti7 7 месяцев назад +1

    perfect

  • @helioobianchi
    @helioobianchi 3 месяца назад +1

    11:30 once you make a setter for an attribute, does a getter become necessary to access it (vice-versa maybe)? is it why it's printing 'undefined'?
    looks like I have some reading and testing to do

  • @maenam4520
    @maenam4520 4 месяца назад

    Greet content 👌
    Why should the age to be >= in the Person class since
    True >= 0 //true
    Just use >= 1
    True >= 1 // false

  • @helioobianchi
    @helioobianchi 3 месяца назад +1

    wait, did Javascript become OOP?
    edit: no. after some googling, I learned that the OOP features introduced in ES6 are syntax sugar. Javascript is classified as a prototype-based procedural language (it's neither object oriented nor functional 😮) . so it's not meant to implement the known OOP design patterns. but it's useful nevertheless.
    can't wait to learn all those features so I can start writing some type related bugs 😊

  • @baxti7
    @baxti7 7 месяцев назад

    If you do an intermediate web project in html and css🙂☺

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

      nothing happens with just html css tech is advancing Javascript,React JS,Next JS etc are crucial to build intermediate and advanced projects

  • @mixxxer
    @mixxxer 2 месяца назад

    What is the benefit of doing get area() versus a method on the class like getArea()?

    • @liammcgarrigle
      @liammcgarrigle 20 дней назад

      because instead of doing ‘console.log( rectangle.getArea() )’ you can do ‘console.log( rectangle.area )’
      so it is like accessing a property and not a method, even though it is like a method is really getting the data under the hood

  • @maenam4520
    @maenam4520 4 месяца назад

    Great job man! 👏👏 that is really awesome and simple explanation straight to my understanding.
    Big thanks 🫶

  • @gergvakapoharat4501
    @gergvakapoharat4501 11 дней назад

    Hi, are getters/setters exportable? I mean if you export myObject.myGetter it exports the value, not the getter itself, so if it changes later, the value won't follow in the export, or copy, etc. thank you

  • @STR-DP
    @STR-DP 3 месяца назад

    37😁

  • @michaelwinkler9257
    @michaelwinkler9257 7 месяцев назад

    1er

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

    Typescript be like..

  • @vanta6lack
    @vanta6lack 2 месяца назад

    420, 69 and pizza. Really? ))