JavaScript Object Methods: Object.keys, assign, freeze, create and More

JavaScript Object Methods: Object.keys, assign, freeze, create and More
0 views

JavaScript Object static methods are some of the most powerful tools in the language — yet many developers use only Object.keys() and Object.assign(). Object.freeze() for immutability, Object.create() for prototype control, Object.fromEntries() for converting Maps — these unlock elegant solutions to common problems. This guide focuses on JavaScript object methods, with the production trade-offs senior engineers need to make sound decisions.

⚡ TL;DR: Object.keys/values/entries for iteration. Object.assign for shallow merge. Object.freeze for immutability. Object.create(null) for hash maps without prototype. Object.fromEntries converts [key,value] pairs back to object. Object.hasOwn replaces hasOwnProperty.

Iteration methods — JavaScript object methods

const user = {name:'Alice',age:30,role:'admin'};
Object.keys(user)    // ['name','age','role']
Object.values(user)  // ['Alice',30,'admin']
Object.entries(user) // [['name','Alice'],['age',30],['role','admin']]

// Transform all values:
const doubled = Object.fromEntries(
  Object.entries(scores).map(([k,v])=>[k,v*2])
);

// Filter object by value:
const passing = Object.fromEntries(
  Object.entries(grades).filter(([,v])=>v>=60)
);

Object.assign — shallow merge

// Merge objects (left to right, last wins)
const config = Object.assign({}, defaults, userConfig);
// Copies enumerable own properties
// SHALLOW: nested objects still shared reference

// Spread alternative (same behavior):
const merged = {...defaults, ...userConfig};

// Clone (shallow):
const clone = Object.assign({}, original);
const clone2 = {...original};

Object.freeze — immutability

const config = Object.freeze({
  API_URL: 'https://api.example.com',
  TIMEOUT: 5000
});
config.API_URL = 'hacked'; // Silent failure in sloppy, TypeError in strict
config.NEW = 'value';     // Silently ignored

// Deep freeze:
const deepFreeze = obj => {
  Object.keys(obj).forEach(k => {
    if(typeof obj[k]==='object' && obj[k]!==null) deepFreeze(obj[k]);
  });
  return Object.freeze(obj);
};

Object.create — prototype control

// Object.create(null) = hash map with no prototype
const safeMap = Object.create(null); // No __proto__, toString, etc.
safeMap['key'] = 'value'; // Safe for any key

// Object.create(proto) = set prototype
const animal = { breathe() { return 'breathing'; } };
const dog = Object.create(animal);
dog.bark = ()=>'woof';
dog.bark();    // 'woof' — own
dog.breathe(); // 'breathing' — from prototype
  • ✅ Object.fromEntries after transforming .entries()
  • ✅ Object.hasOwn(obj,key) over obj.hasOwnProperty(key)
  • ✅ Object.freeze for config constants
  • ✅ Object.create(null) for safe hash maps
  • ❌ Object.assign is shallow — nested objects share reference
  • ❌ Object.freeze is shallow — nested objects still mutable

External reference: MDN Object documentation.

Recommended Reading

→ Designing Data-Intensive Applications — The bible of distributed systems and production engineering at scale.

→ The Pragmatic Programmer — Timeless engineering wisdom every senior developer needs.

Affiliate links. We earn a small commission at no extra cost to you.

Free Weekly Newsletter

🚀 Join 2,000+ Senior Developers

Get expert-level JavaScript, Python, AWS, system design and AI secrets every week. Zero fluff, pure signal.

✓ No spam✓ Unsubscribe anytime✓ Expert-level only

Related: JavaScript Array Methods: Every Method Explained With Real Examples · JavaScript Proxy Object: The Most Underused Feature in the Language · TypeScript for JavaScript Developers: The Migration Guide That Actually Works

Related: JavaScript Array Methods: Every Method Explained With Real Examples · JavaScript Closures, Scope, and the Module Pattern: The Complete Guide · JavaScript Promises: The Complete Guide From Basics to Advanced Patterns

Deep-dive PDF

Get the expanded guide for this post — extra diagrams-style checklists, failure modes, and a production walkthrough. Free when you subscribe to CheatCoders.

Already subscribed? or open the subscribe page.


Discover more from CheatCoders

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a comment

No account needed. Name and email are optional.