惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
IT之家
IT之家
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Understanding JavaScript Classes Objects and the ‘new’ Ke...
Nabin Kandel · 2026-05-10 · via DEV Community
Cover image for Understanding JavaScript Classes Objects and the ‘new’ Keyword: A Beginner’s Guide

Nabin Kandel

JavaScript Classes & the new Keyword: A Beginner's Guide to OOP 🎯

If you're just starting with JavaScript and Object-Oriented Programming (OOP), the class and new keywords might seem confusing. Let me break it down in the simplest way possible.


📐 What is a Class?

Think of a class as a blueprint or a recipe. It doesn't contain actual data — it just describes what kind of data an object should have.

class Student {
  name;
  age;
  gender;
}

Enter fullscreen mode Exit fullscreen mode

This Student class is like saying: "Every student should have a name, age, and gender."

But we haven't created any actual students yet—just the plan.


🛠️ Creating Objects from a Class

To create actual students (objects) from our blueprint, we use the new keyword:

const ram = new Student();
ram.name = "Ram";
ram.age = 10;
ram.gender = "Male";

const sita = new Student();
sita.name = "Sita";
sita.age = 9;
sita.gender = "Female";

const hari = new Student();
hari.name = "Hari";
hari.age = 11;
hari.gender = "Male";

Enter fullscreen mode Exit fullscreen mode

✅ Now we have three real student objects, each with their own data.


🔍 Using the Data

Once created, you can access each student's information:

console.log(ram.name);    // Output: Ram
console.log(sita.age);    // Output: 9
console.log(hari.gender); // Output: Male

Enter fullscreen mode Exit fullscreen mode


❓ Why Do We Need the new Keyword?

This is where most beginners get confused.

Why can't we just write const ram = Student()?

Without new

const ram = Student();  // ❌ Error! This won't work

Enter fullscreen mode Exit fullscreen mode

JavaScript doesn't understand that you want to create an object. It thinks you're trying to call a regular function.

With new

const ram = new Student();  // ✅ This works!

Enter fullscreen mode Exit fullscreen mode

The new keyword tells JavaScript:

"I want to create a brand new object based on this class."


⚙️ What Does new Actually Do?

When you use new Student(), JavaScript performs three important steps:

  1. 📦 Creates an empty object
  2. 🔗 Links it to the Student class (so it knows what properties/methods it should have)
  3. Returns the new object (and stores it in your variable)

🍪 A Real-World Analogy

Concept Real-World Equivalent
Class Cookie cutter 🍪
new Pressing the cutter into dough
Object The actual cookie you can eat

You can use the same cookie cutter (class) to make many cookies (objects), but each cookie is separate and can have different decorations (data).


📋 Quick Reference

Concept What It Is Example
Class Blueprint/Template class Student { }
Object Actual instance with data const ram = new Student()
new Keyword to create objects new Student()

🔑 Key Takeaways

  • One class can create many objects
  • Each object has its own independent data
  • The new keyword is required to create objects from a class

💻 Complete Example

Here's everything together:

// 1️⃣ Define the blueprint
class Student {
  name;
  age;
  gender;
}

// 2️⃣ Create objects using 'new'
const ram = new Student();
ram.name = "Ram";
ram.age = 10;
ram.gender = "Male";

const sita = new Student();
sita.name = "Sita";
sita.age = 9;
sita.gender = "Female";

// 3️⃣ Use the data
console.log(ram.name);   // Ram
console.log(sita.age);   // 9

// 4️⃣ Each object is independent
ram.age = 11;            // Only changes ram's age
console.log(ram.age);    // 11
console.log(sita.age);   // Still 9 👍

Enter fullscreen mode Exit fullscreen mode


Tags: #JavaScript #WebDevelopment #Programming #OOP #Beginners #LearnToCode