Spencer Lockhart
Sep 5, 2018 · 1 min read

That 2nd approach is actually an error.. But if you meant this below, there is a difference:

class Cat {
constructor () {
// 2nd approach
this.walking = () => console.log('cat walk');
}
// 1st approach
meow () {
console.log(‘meow’)
}
}
A different walking function is created for every new Cat, while the meow function is shared by all the new Cats.

The walking method is actually part of the new Cat, rather than the new Cat’s prototype.

It can no longer meow, but can still walk. Also, when we change the __proto__ of cat, it’s no longer an instance of Cat.

If we change the prototype of our new cat (never do this!), it will be able to walk, but not meow.

It’s worth noting that a new walking function is created for every new cat. In contrast, the prototype is shared by all the cats, and only the context (what this points to) is unique for new cats.

In practice, though, cat.walking() and cat.meow() both do what you would expect. So, use both approaches when it makes sense to (set properties in the constructor using this , and methods outside of the constructor where possible).

    Spencer Lockhart

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade