Object
- In Turkish
- Nesne
- Pronunciation
- OB-jekt
In short
An object is a bundle of related data and behavior: values in named fields, often called properties, and functions that work on them, called methods.
What is an object in programming?
Objects let a program model things the way people think about them. A user object might hold a name, an email address and a signup date, and offer methods such as rename() or isActive(). Instead of passing loose variables around, the code passes one object that keeps its data and the operations on that data together.
In class-based languages such as Java, C#, Python and C++, an object is an instance of a class: the class is the blueprint, and each object made from it has its own copy of the fields. new User("Ada") and new User("Linus") are two objects of the same class with different data. In JavaScript, objects can also be written directly as literals, { name: "Ada" }, and inherit through prototypes.
Variables usually hold a reference to an object rather than the object itself. Assigning an object to a second variable doesn't copy it: both names point to the same object, so a change through one is visible through the other. Comparing two objects with == or === in many languages checks whether they are the same object, not whether their contents match.
A common misconception is that objects belong only to object-oriented programming. Functional and procedural code also use objects or records to group data; what makes code object-oriented is organizing it around objects that hide their internal state and expose behavior through methods.
Key takeaways
- An object groups related data (properties) and behavior (methods).
- In class-based languages an object is an instance of a class.
- JavaScript also has object literals and prototype-based inheritance.
- Variables hold references, so two names can point to the same object.
- Object-oriented code is organized around objects that hide their state.
Example
class User {
constructor(name) {
this.name = name; // a property
this.createdAt = new Date();
}
greet() { // a method
return `Hi, I'm ${this.name}`;
}
}
const ada = new User("Ada"); // an instance of User
console.log(ada.greet());
const point = { x: 2, y: 3 }; // an object literal
const same = point; // a second reference, not a copy
same.x = 10;
console.log(point.x); // 10Readers ask
What is the difference between a class and an object?
A class is the definition: which fields and methods its objects have. An object is one concrete instance made from it, with its own values. One class can produce any number of objects.
What are properties and methods?
Properties, also called fields or attributes, are the values an object holds. Methods are the functions that belong to the object and usually read or change those values.
How do I copy an object?
Assignment only copies the reference. To get a new object, make a copy explicitly: a shallow copy duplicates the top-level fields, such as { ...obj } in JavaScript, while a deep copy, such as structuredClone(obj), also copies nested objects.
Often compared
See also
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- MethodProgramming Fundamentals, p. 37A method is a function that belongs to an object or class and is called on it, so it can read and change that object's data, as in user.rename("Ada").
- EncapsulationProgramming Fundamentals, p. 17Encapsulation is the practice of bundling data with the methods that use it and hiding internal details, so outside code works only through a public interface.
- InheritanceProgramming Fundamentals, p. 28Inheritance is an object-oriented programming feature that lets a new class reuse, extend, and override the fields and methods of an existing class.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- ConstructorProgramming Fundamentals, p. 13A constructor is a special method that runs when a new object is created from a class, setting up its starting values so it is ready to use.
Spotted a mistake or something missing on this page?Suggest an edit