- Abstraction
- AI Pair Programming
- Algorithm
- API
- Array
- Array methods
- Booleans
- Callback
- Class
- Class Members
- Closure
- Closure
- Code refactoring
- Comment
- Computer programming
- Conditional statements
- Constant
- Constructor
- Coupling and Cohesion
- Data types
- Debugging
- Decorator
- Dependency
- Destructuring
- Dictionary
- Enum
- Event
- Exception / Error handling
- Function
- Generic / Template
- Higher-order function
- IDE
- Immutability
- Inheritance
- Input validation
- Integer
- Interface
- Iteration patterns
- Legacy code
- Loop
- Machine learning
- Memoization
- Memory and references
- Method
- Module
- Null / Undefined / None
- Null safety / Optional values
- Object
- Object-Oriented Programming (OOP)
- Operator
- Parameter
- Parsing
- Promise and Async/Await
- Prompt Engineering
- Recursion
- Regular expression
- Return statement
- Rollback
- Runtime
- Scope
- Script
- Sequence
- Set
- Spaghetti code
- Spread and Rest operators
- State management
- String
- Switch statement
- Synchronous vs Asynchronous execution
- Syntax
- Technical debt
- Ternary operator
- Testing
- This / Self
- Tuple
- Type casting
- Type conversion
- Variable
- Vibe coding
- Webhook
PROGRAMMING-CONCEPTS
Class Members: Syntax, Usage, and Examples
Class members are the individual pieces that make up a class — such as properties, methods, static members, getters, setters, and access modifiers. They define what information an object holds and what actions it can perform, and they shape how other parts of your program interact with it.
Understanding class members is essential for working confidently with object-oriented code in JavaScript, TypeScript, Python, and Swift.
Instance Members
Instance members belong to each individual object created from a class. Every instance gets its own copy of these properties, which means changing one object’s state doesn’t affect another.
JavaScript / TypeScript Example
class Book {
title: string;
pages: number;
constructor(title: string, pages: number) {
this.title = title;
this.pages = pages;
}
describe() {
return `${this.title} has ${this.pages} pages.`;
}
}
const guide = new Book("JavaScript Guide", 320);
guide gets its own title and pages, and calling describe() uses the values stored on that instance. Each new Book object would have its own independent data.
Python Example
Python
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
def describe(self):
return f"{self.title} has {self.pages} pages."
guide = Book("Python Guide", 280)
Every created Book carries its own properties, so modifying one instance won’t affect others. Instance methods automatically receive self, giving them access to that specific object.
Swift Example
class Book {
var title: String
var pages: Int
init(title: String, pages: Int) {
self.title = title
self.pages = pages
}
func describe() -> String {
return "\(title) has \(pages) pages."
}
}
Swift binds each stored property to the instance, and methods operate on that instance’s values. Creating multiple books produces separate objects with independent state.
Static Members
Static members belong to the class itself, not the instances. They’re useful for shared utilities, counters, and configurations.
JavaScript / TypeScript Example
class MathTools {
static multiply(a: number, b: number) {
return a * b;
}
}
MathTools.multiply(4, 6);
multiply is called on the class rather than an instance, making it a general-purpose helper. Static members never need this because they aren’t tied to a specific object.
Python Equivalent
Python
class MathTools:
@staticmethod
def multiply(a, b):
return a * b
Using @staticmethod marks a method that doesn’t depend on instance state. It behaves like a regular function wrapped inside the class for organization.
Swift Equivalent
class MathTools {
static func multiply(_ a: Int, _ b: Int) -> Int {
return a * b
}
}
Swift’s static keyword signals that the method belongs to the type. You call it directly on the class, not on an instance.
Getters and Setters
Getters and setters allow controlled access to properties — useful for validation, formatting, or generating values on demand.
JavaScript / TypeScript Example
class Temperature {
private _celsius: number;
constructor(celsius: number) {
this._celsius = celsius;
}
get fahrenheit() {
return this._celsius * 1.8 + 32;
}
set celsius(value: number) {
if (value < -273.15) throw new Error("Below absolute zero");
this._celsius = value;
}
}
const t = new Temperature(20);
The getter calculates Fahrenheit only when accessed, and the setter adds validation before updating Celsius. This pattern keeps internal data safe while exposing a clean interface.
Python Example
Python
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def fahrenheit(self):
return self._celsius * 1.8 + 32
@fahrenheit.setter
def fahrenheit(self, value):
self._celsius = (value - 32) / 1.8
Python uses @property decorators to define getters and setters. This allows attribute access while still controlling updates behind the scenes.
Swift Example
class Temperature {
private var celsius: Double
init(_ celsius: Double) {
self.celsius = celsius
}
var fahrenheit: Double {
return celsius * 1.8 + 32
}
}
Computed properties in Swift update automatically whenever accessed. There’s no need for explicit getter syntax — the var body handles it.
Access Modifiers
Access modifiers control which parts of your program can access or modify a class’s members. They help protect internal logic and maintain clean boundaries.
JavaScript / TypeScript
class Account {
public owner: string;
private balance: number;
constructor(owner: string, balance: number) {
this.owner = owner;
this.balance = balance;
}
deposit(amount: number) {
this.balance += amount;
}
}
public makes owner accessible everywhere, while private hides balance from outside modification. This ensures deposits happen only through the deposit method.
Python (informal)
Python
class Account:
def __init__(self, owner, balance):
self.owner = owner
self._balance = balance
Python doesn’t enforce access levels strictly, but prefixing _balance signals “internal use only.” The convention encourages good structure even without hard restrictions.
Swift
class Account {
public var owner: String
private var balance: Double
init(owner: String, balance: Double) {
self.owner = owner
self.balance = balance
}
}
Swift enforces modifiers such as public, internal, and private, giving you fine control over visibility. These guardrails prevent accidental misuse of sensitive properties.
Computed Properties
Computed properties don’t store values; they calculate and return them on demand based on other properties.
JavaScript / TypeScript Example
class Rectangle {
constructor(private width: number, private height: number) {}
get area() {
return this.width * this.height;
}
}
The rectangle never stores area; it calculates the value each time it's accessed. This avoids duplicating data and keeps the property always up to date.
Swift Example
class Rectangle {
var width: Double
var height: Double
var area: Double {
return width * height
}
init(width: Double, height: Double) {
self.width = width
self.height = height
}
}
Swift’s computed properties look like normal variables, but they run code behind the scenes. This makes them great for values that depend on other pieces of state.
Python Equivalent (property)
Python
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
@property
def area(self):
return self.width * self.height
Python’s @property decorator provides the same behavior. No separate field is stored — the value is always derived from the current width and height.
Summary
Class members define the structure and behavior of both classes and the objects created from them. Instance members store object-specific data, static members provide shared utilities, getters and setters allow controlled access to properties, access modifiers shape visibility, and computed properties generate values dynamically. Together, these pieces form the foundation of object-oriented programming in JavaScript, TypeScript, Python, and Swift.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.