Are there any differences between Class and Object Functions defining methods?
Solution 1:
A js class is a beautified way of writing your first bit of code. So yes these two syntaxes do exactly the same thing.
Solution 2:
class
in JavaScript is a syntactic sugar of constructor functions. When you work with functions, you are dealing with prototypes and prototypal inheritance, whereas in a class
it's more of coupling these actions together within a blueprint, which again makes use of functions and prototypes under the hood. In the first case, the function greet
is a prototype property of the function Dog
, meaning the instance of Dog
will be able to access the method greet
, and the same applies to the second case as well. (Make sure to use the new
keyword when instantiating objects in both cases).