DEV Community

Cover image for JavaScript Classes
Hanna
Hanna

Posted on

3

JavaScript Classes

A bit about class in js.

Alt Text

Class Methods

class Animal {
  constructor(name, legs) { ... }
  speech() { ... }
  numberOfLegs() { ... }
}
const dog = new Animal();
const cat = new Animal();
const spider = new Animal();
Enter fullscreen mode Exit fullscreen mode

The code above defines a class ClassName. The curly braces { } delimit the class body.
The class becomes useful when you create an instance of the class (dog, cat, spider). An instance is an object containing data and behavior described by the class.

ALWAYS add a method named constructor(). It is executed automatically when a new object is created and is used to initialize object properties.
Animal’s constructor has two parameters name and legs, which is used to set the initial value of the fields this.name, this.legs.

new Animal() creates instances of the Animal class.

                    SO...

"CLASS is used to describe one or more objects. 
It serves as a template for creating, or 
instantiating, specific objects within a program. 
While each object is created from a single class, 
one class can be used to instantiate multiple 
objects."
Enter fullscreen mode Exit fullscreen mode

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Image of DataStax

Langflow: Simplify AI Agent Building

Langflow is the easiest way to build and deploy AI-powered agents. Try it out for yourself and see why.

Get started for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay