#atom
Content:
Objects in JavaScript are collections of key-value pairs, where keys are strings (or Symbols) and values can be any data type, including other objects, functions, arrays, or primitives. Objects are fundamental to JavaScript and are used to represent structured data. They are also the building blocks for more advanced concepts like prototypes and classes.
Key Characteristics:
- Objects are dynamic: properties can be added, modified, or deleted at runtime.
- Objects are reference types: when assigned to a variable or passed to a function, they are passed by reference.
- Objects inherit properties and methods from their prototype.
Common Object Methods (with TypeScript Signatures):
Object.keys()
: Returns an array of a given object's own enumerable property names.- Signature:
keys(o: object): string[];
- Signature:
Object.values()
: Returns an array of a given object's own enumerable property values.- Signature:
values(o: object): any[];
- Signature:
Object.entries()
: Returns an array of a given object's own enumerable key-value pairs.- Signature:
entries(o: object): [string, any][];
- Signature:
Object.assign()
: Copies the values of all enumerable properties from one or more source objects to a target object.- Signature:
assign<T, U>(target: T, source: U): T & U;
- Signature:
Object.create()
: Creates a new object with the specified prototype object and properties.- Signature:
create(proto: object | null, propertiesObject?: PropertyDescriptorMap): any;
- Signature:
Object.hasOwnProperty()
: Returns a boolean indicating whether the object has the specified property as its own property (not inherited).- Signature:
hasOwnProperty(v: PropertyKey): boolean;
- Signature:
Object.freeze()
: Freezes an object, preventing new properties from being added and existing properties from being modified or deleted.- Signature:
freeze<T>(o: T): Readonly<T>;
- Signature:
Object.seal()
: Prevents new properties from being added and marks all existing properties as non-configurable.- Signature:
seal<T>(o: T): T;
- Signature:
Prototypes and Inheritance:
- Every object in JavaScript has a hidden
[[Prototype]]
property, which points to another object ornull
. - The
[[Prototype]]
chain is used for inheritance: if a property or method is not found on an object, JavaScript looks up the prototype chain. - The
Object.prototype
object is at the top of the prototype chain for most objects. - Methods like
Object.create()
andObject.setPrototypeOf()
allow explicit manipulation of an object's prototype.
Example:
const person = {
name: "Alice",
greet() {
console.log(`Hello, my name is ${this.name}`);
}
};
const student = Object.create(person);
student.major = "Computer Science";
student.greet(); // "Hello, my name is Alice"
Linked Cards:
- Prototypes in JavaScript: Objects inherit properties and methods from their prototype.
- Classes in JavaScript: Classes in JavaScript are syntactic sugar over prototype-based inheritance.
- Functions in JavaScript: Functions can be used as methods within objects.
- [[The
this
Keyword in JavaScript]]: Thethis
keyword is often used in object methods to refer to the object itself. - Data Types in JavaScript: Objects are a composite data type in JavaScript.
- Syntactic Sugar: Classes and modern object methods provide syntactic sugar over traditional prototype-based patterns.
Tags: #JavaScript #Objects #Prototypes #Methods #TypeScript #Inheritance
Connections:
Sources: