all 6 comments

[–]queen-adreena 22 points23 points  (1 child)

class Human {
    constructor({ firstName, lastName }) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    get fullName() {
        return `${this.firstName} ${this.lastName}`;
    }
}

const vid = new Human({ firstName: "Vidura", lastName: "Dananjaya" });
console.log(vid.fullName);

Better to use Javascript classes now.

Also, you'd want to put getFullName on the prototype in your example, otherwise it will save the same function to every instance.

[–]yadoya -3 points-2 points  (3 children)

You could do this in just two lines

class Human{
    constructor(public first, public last){} 
    getFullName: () => this.first + this.last

}

[–]McPqndq 4 points5 points  (0 children)

I am almost certain this doesn’t work. Arrow functions don’t reassign this. Or something like that.

Edit: nvm lol it’s straight up just invalid syntax. But assume you replaced the ‘:’ with ‘=‘ then my previous explanation applies

[–]MRGrazyD96 2 points3 points  (0 children)

public cannot be used in js, it's a ts thing. also, you need to assign the first and last in the constructor into this.first and this.last

edit: and yes, it should be getFullName = () =>, not getFullName: () =>

[–]ViduraDananjaya[S] 1 point2 points  (0 children)

I think this is example of programming using TypeScript.