Programming README
Home
Comparison
Aspect | Class | Object | Property | Method |
---|
Definition | A blueprint for creating objects. | An instance of a class. | A variable that holds data within a class or object. | A function defined within a class that operates on the object’s properties. |
Role | Defines the structure and behavior of objects. | Represents an entity with its own state and behavior. | Represents the data/state of an object. | Defines actions/behavior for an object. |
Nature | Template/blueprint for objects. | Concrete instance with actual data. | Data specific to an object. | Actions that can be performed by an object. |
Context | Used to create objects. | Holds data and methods as defined by its class. | Data members of a class or object. | Functions that manipulate object properties. |
Creation | Defined using the class keyword. | Created using the new keyword followed by a class constructor. | Declared within a class. | Declared within a class. |
Example | class Person { ... } | const person1 = new Person("Alice", 30); | person1.name | person1.greet() |
Detailed Examples
Class
Object
1. Object Literal
The most straightforward way to create an object is using an object literal.
2. Using Interfaces
You can define the shape of an object using an interface and then create an object that adheres to that interface.
3. Using Classes
You can define a class and create an instance of that class.
4. Using Object.create
You can create an object with a specified prototype using Object.create.
5. Using Type Aliases
Similar to interfaces, you can use type aliases to define the shape of an object.
6. Using Constructors with Interfaces
You can combine constructors and interfaces to create objects.
7. Using Factory Functions
You can use factory functions to create objects.
8. Using Enums
You can use enums to create objects with predefined values.
9. Using Mapped Types
You can use mapped types to create objects dynamically.
Property
Method
Programming README
Home