JavaScript isn't an OOP language. Many beginners think of JavaScript classes as “classical OOP”, similar to Java or C++. But here’s the fascinating part: ✅ JavaScript doesn’t have classical inheritance. ✅ Classes in JS are syntactic sugar over its prototype system. Here’s what actually happens: class User { greet() { console.log('Hello!'); } } const u = new User(); u.greet(); When you create `u`, JS doesn’t copy `greet` into `u`. Instead: - `u` has a hidden reference `__proto__` pointing to `User.prototype`. - The method `greet` lives on the prototype. - JS looks up the prototype chain when a method is called. You can even add new methods dynamically: User.prototype.sayBye = () => console.log('Bye!'); u.sayBye(); 💡 Takeaway: Understanding prototypes is the key to truly mastering JS OOP. Classes are just a convenient way to write what prototypes already do. #JavaScript #WebDevelopment #OOP #Prototypes #LearningInPublic
JavaScript OOP: Classes are just syntactic sugar
More Relevant Posts
-
JavaScript OOP: The Truth Behind “Classes” A common misconception is that JavaScript uses "real" classes like Java or C++. In reality, JavaScript is built on prototypal inheritance - or more accurately, behavior delegation. Every object in JavaScript has an internal link to another object called its prototype. When you try to access a property or method that doesn’t exist on the object itself, JavaScript looks for it up the prototype chain. This process isn’t about copying properties or methods - it’s about delegating behavior. Think of it this way: Objects in JavaScript don’t inherit behavior in the classical sense - they delegate behavior to other objects. This delegation model is far more flexible and dynamic than traditional class-based inheritance, allowing objects to share behavior at runtime without rigid hierarchies. Understanding this concept changes how you architect and reason about your code. Instead of forcing class-based design patterns from other languages, you start leveraging JavaScript’s native strengths: composition, delegation, and dynamic object links. Currently diving deeper into these mechanics through “You Don’t Know JS: Objects & Classes” - a must-read for truly understanding how JavaScript thinks. Frameworks evolve. Syntax evolves. But understanding how the language itself works - that remains constant. #JavaScript #OOP #SoftwareEngineering #ComputerScience #Learning
To view or add a comment, sign in
-
Functional Programming in JavaScript — The Secret Sauce Behind Cleaner Code At first glance, JavaScript looks like Java or C++. But deep down, it’s far closer to functional programming languages like Lisp, Scheme, or ML. Why? Because JS has first-class functions — meaning: ✅ Functions behave like objects 🧩 ✅ You can pass them as arguments ✅ You can return them from other functions This is what unlocks Functional Programming (FP) — a way to write cleaner, reusable, and expressive code. 🧠 Example: Doubling an Array ❌ Imperative (traditional) way: const arr = [1, 2, 3]; let doubled = []; for (let i = 0; i < arr.length; i++) { doubled.push(arr[i] * 2); } console.log(doubled); // [2, 4, 6] ✅ Functional way: function mapForEach(arr, fn) { let newArr = []; for (let i = 0; i < arr.length; i++) { newArr.push(fn(arr[i])); } return newArr; } const arr = [1, 2, 3]; const doubled = mapForEach(arr, item => item * 2); console.log(doubled); // [2, 4, 6] Instead of telling JavaScript how to loop, you just tell it what to do for each element. 🌟 Why It Matters 🔹 Reusability: Same mapForEach can filter, transform, or validate. 🔹 Cleaner code: No boilerplate loops. 🔹 Composability: Functions can be chained to build complex logic. 💡 Pro Tip: In FP, avoid mutating data — always return new values. Immutability = predictability = fewer bugs. 👉 Functional programming isn’t a fancy buzzword — It’s what makes JavaScript powerful, elegant, and fun to write. What’s your favorite functional trick in JS — map, reduce, or filter? Let’s discuss 👇 #JavaScript #FunctionalProgramming #WebDevelopment #CodeBetter #LearningJS
To view or add a comment, sign in
-
JavaScript taught me something cool today 🤔 You know how we write classes in JavaScript? Turns out, JS doesn't actually have "real" classes like Java or C++. It's a Prototype-based language wearing a class costume! ✨ ➡️When we write this: class Person { constructor(name) { this.name = name; } greet() { return `Hello, I'm ${this.name}`; } } ➡️ JavaScript actually does this: function Person(name) { this.name = name; } Person.prototype.greet = function() { return `Hello, I'm ${this.name}`; } 🔺 Same result, different reality! The class syntax is just syntactic sugar to make it look familiar. Under the hood, everything still runs on Prototypes, implementing Inheritance, Encapsulation, Abstraction and Polymorphism in its own unique way. 📌 Understanding this helped me: • Finally get why this acts weird sometimes • Debug inheritance issues way faster • Realize methods live on the prototype (memory efficient!) 🖊️ Anyone else had this "wait, what?" moment with JavaScript? 😅 #JavaScript #WebDevelopment #LearningToCode
To view or add a comment, sign in
-
🚀 𝐔𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝𝐢𝐧𝐠 𝐓𝐲𝐩𝐞𝐬 𝐨𝐟 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐢𝐧 𝐣𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 🚀 In JavaScript, functions can be written in several ways, and each type has its own purpose. Function Declaration is the basic way to create a function using the function keyword. It can be called before it’s defined. ✨ 𝐍𝐚𝐦𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 — A regular function with a name, defined using the function keyword. It can be called multiple times and improves code readability. 🕒 𝐀𝐧𝐨𝐧𝐲𝐦𝐨𝐮𝐬 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 — A function without a name, often used inside other functions as callbacks like in setTimeout or addEventListener. ⚡ 𝐀𝐫𝐫𝐨𝐰 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 — A modern and shorter syntax that makes code cleaner and inherits this from its surrounding scope. 🚀 𝐈𝐈𝐅𝐄 (𝐈𝐦𝐦𝐞𝐝𝐢𝐚𝐭𝐞𝐥𝐲 𝐈𝐧𝐯𝐨𝐤𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧) — A function that runs immediately after it’s defined. It’s useful to keep variables private and avoid polluting the global scope. 🧠 𝐇𝐢𝐠𝐡𝐞𝐫-𝐎𝐫𝐝𝐞𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 — A function that takes another function as an argument or returns a function. This is often used in functional programming, like with map, filter, or forEach. 🏗️ 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 — A special type of function used with the new keyword to create and initialize objects. 🌟 Great insights from Codexign, worth sharing! #javascript #js #softwaredeveloper #JavaScriptTips #Functions #FunctionsInJavaScript #JSTutorial #webdevelopment #frontenddevelopment #CodingTips #JavaScriptDeveloper #JavaScriptTypes #Programming #Coding #JSLearning #JSTypes #education #LearnJavaScript #technology #w3schools #careers
To view or add a comment, sign in
-
Today I revised some important OOP(Object Oriented Programming) concepts in JavaScript -- and here is what I learned. 1. PROTOTYPES In JavaScript, prototypes allow one object to use the properties and methods of another object using" .__proto__ ". If both the object and it's prototype have the same method, the object's own method gets priority during execution. 2. CONSTRUCTOR The constructor is a special reserved keyword in JS classes. It automatically executes when an object is created, initialising it's properties right from the start. 3. INHERITANCE Inheritance helps a class use properties and methods from another class using the extends keyword -- like "class child extends parent". 4. SUPER KEYWORD When both parent and child classes have constructor, we use the super keyword in the child class to call the parent's constructor and pass it's parameters efficiently. Overall, this revision really helped me understand how JavaScript handles OOP behind the scenes and how flexible prototypes and inheritance make our code.
To view or add a comment, sign in
-
🌱 Understanding Two Subtle Yet Powerful JavaScript Concepts: Garbage Collection & Optional Chaining (?.) When learning JavaScript deeply, it’s often the hidden mechanisms that make the language truly elegant. Let’s explore two of them — Garbage Collection and Optional Chaining (?.). 🧹 Garbage Collection — The Invisible Cleaner Every time we create an object, JavaScript allocates memory for it. But here’s the key: JavaScript automatically frees memory that’s no longer reachable. Example: let user = { name: "Gautam" }; user = null; // The object is now unreachable Once there’s no reference to the object, it becomes garbage — and the Garbage Collector quietly removes it from memory. You don’t need to manually manage memory like in C or C++. However, understanding this helps you write memory-efficient and cleaner code. 💡 Tip: Keep your data structures minimal and avoid unnecessary global references. What isn’t referenced can be beautifully forgotten — by design. 🔗 Optional Chaining (?.) — Safe and Elegant Access We’ve all faced this error: TypeError: Cannot read properties of undefined That’s where Optional Chaining shines. let user = {}; console.log(user.address?.street); // undefined, not an error If user.address doesn’t exist, JavaScript stops and simply returns undefined. It’s like saying: “If the path exists, go ahead; if not, no worries.” This makes our code safer, cleaner, and more expressive. ✨ Learning Takeaway Both of these concepts teach us something beyond syntax: <>Garbage Collection reminds us that letting go of what’s no longer needed is healthy — in code and in life. <>Optional Chaining shows that defensive programming doesn’t have to be messy — it can be graceful. 📚 Inspired by javascript.info, one of the best resources for understanding JavaScript deeply. #JavaScript #WebDevelopment #Learning #CleanCode #Programming #FrontendDevelopment
To view or add a comment, sign in
-
Advanced Beginner’s guide to ClojureScript So how does your tiny ClojureScript program transform into runnable JavaScript? (ns app.core) (println "Howdy!") First of all, you need a compiler! ClojureScript is a hosted language, which means you have to compile it into JavaScript, unless you want to ship the compiler into a browser to be able to interpret ClojureScript at runtime, which is slow and in most cases doesn't make sense (but maybe you wanna build online REPL, that's ok). This is not going to be easy. ClojureScript sits on top of JavaScript, the compiler is a library written in Clojure, which in turn hosts on JVM. This means you need Java installed, huh. I prefer to use sdkman to manage my Java installations. sdk install java Next step is to install Clojure, head over to installation guide at clojure.org. If the following command returns 2, you are good! clj -M -e "(inc 1)" Create project directory somewhere and put deps.edn file into it with the following contents. deps.edn is kind of package.json, if you are coming https://lnkd.in/g9RtJYyC
To view or add a comment, sign in
-
JavaScript Deep Dive: The Story Behind null Today, I explored the origins of one of JavaScript’s quirkiest features — null. It turns out this little value has a surprising history! 🔹 Where it Began: JavaScript was created in 1995 by Brendan Eich in just 10 days. Since it was inspired by Java, null was included to represent the intentional absence of any object — just like in Java. 🔹 The Famous Bug: Ever wondered why this happens? typeof null // "object" This result is actually due to an early implementation bug. null was given the same type tag as objects — and fixing it would have broken lots of existing code. So it stayed. 😅 🔹 null vs undefined: JavaScript defines two empty values: null: Intentionally empty (set by programmers) undefined: Automatically assigned by JavaScript for missing or uninitialized values This distinction has confused developers for decades! 🔹 Standardized in ECMAScript: Both null and undefined were formalized in the first ECMAScript standard (1997), cementing their roles in the language. 💡 Takeaway: JavaScript's null is a great example of how early design decisions — even mistakes — can shape a language's future. Knowing its history helps us write code with more clarity and intention. Let’s keep learning the story behind the code! 🎯 #JavaScript #LearningInPublic #WebDevelopment #HistoryOfJS #Programming #10000Coders #CodeNewbie
To view or add a comment, sign in
-
-
“The beauty (and chaos) of JavaScript 😅” One thing I still find amazing about JavaScript — it can behave like any language you want it to be. You can write it like Python, make it feel like Java, or go full chaos mode and write something only you can understand 😂 Arrow functions, callbacks, async/await, closures — JS gives you the freedom to be elegant or confusing, depending on how much coffee you’ve had ☕ That’s what makes it both loved and hated — it’s simple to start, but endless to master. ✨ JavaScript is beautiful. #JavaScript #WebDevelopment #Frontend #DevHumor #CodingJourney #DeveloperLife
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development