#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:

Common Object Methods (with TypeScript Signatures):

  1. Object.keys(): Returns an array of a given object's own enumerable property names.
    • Signature: keys(o: object): string[];
  2. Object.values(): Returns an array of a given object's own enumerable property values.
    • Signature: values(o: object): any[];
  3. Object.entries(): Returns an array of a given object's own enumerable key-value pairs.
    • Signature: entries(o: object): [string, any][];
  4. 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;
  5. Object.create(): Creates a new object with the specified prototype object and properties.
    • Signature: create(proto: object | null, propertiesObject?: PropertyDescriptorMap): any;
  6. Object.hasOwnProperty(): Returns a boolean indicating whether the object has the specified property as its own property (not inherited).
    • Signature: hasOwnProperty(v: PropertyKey): boolean;
  7. 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>;
  8. Object.seal(): Prevents new properties from being added and marks all existing properties as non-configurable.
    • Signature: seal<T>(o: T): T;

Prototypes and Inheritance:

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:

Tags: #JavaScript #Objects #Prototypes #Methods #TypeScript #Inheritance

Connections:


Sources: