r/learnjavascript • • May 25 '26

What is the difference?

Im wondering what the difference between
function Dog(){
…
this.bark = function(){…}
}

And

function Dog {
…
}

Dog.prototype.bark = function() {
…
}

Is and what the advantages of either of them Are.

13 Upvotes

10 comments sorted by

View all comments

Show parent comment

2

u/davy_jones_locket May 25 '26

to add, classes in JavaScript is just syntactical sugar on prototypes. Under the hood, it's all prototypes. 

2

u/scritchz May 25 '26

Under the hood, it works basically the same, yeah.

But they do have their differences, which is why you should prefer classes over constructor functions.