# Advanced Object Patterns and Mixins

> Explain Object.create(), and mixin patterns in JavaScript.

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-object-patterns
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

Explain Object.create(), and mixin patterns in JavaScript.

## Answer

Object.create(proto) creates a new object with the specified prototype, enabling prototypal inheritance without constructors.

Mixins extend objects with behavior from multiple sources - a way to compose functionality without deep inheritance chains.

Object.assign(target, ...sources) shallow-copies enumerable own properties from sources into target.

OLOO (Objects Linked to Other Objects) by Kyle Simpson prefers prototypal delegation over class-based inheritance.

**Shadowing a shared method. Toggle rex's own speak under Tweak and watch where the lookup stops.**

1. **Lookup stops at the first match**: An own property on rex hides the shared one.
2. **Mixins copy onto prototypes**: Object.assign(Dog.prototype, mixin) puts methods where every instance can reach them.
3. **hasOwnProperty**: Tells an own property from an inherited one.

## Code examples

### Object.create and Prototypal Inheritance

```
const animal = {
  breathe() { return `${this.name} breathes`; },
  describe() { return `I am ${this.name}`; }
};

const dog = Object.create(animal);
dog.bark = function() { return `${this.name} barks`; };

const myDog = Object.create(dog);
myDog.name = 'Rex';

console.log(myDog.breathe());
console.log(myDog.bark());

console.log(Object.getPrototypeOf(myDog) === dog);
console.log(Object.getPrototypeOf(dog) === animal);

const pureMap = Object.create(null);
pureMap.key = 'value';
console.log(pureMap.hasOwnProperty);
```

Object.create() sets up the prototype chain explicitly. Object.create(null) creates a pure dictionary with no inherited methods.

### Mixin Factory Pattern

```
const Serializable = (Base) => class extends Base {
  serialize() { return JSON.stringify(this); }
};

const Timestamped = (Base) => class extends Base {
  constructor(...args) {
    super(...args);
    this.createdAt = Date.now();
  }
};

const Validatable = (Base) => class extends Base {
  validate() {
    return Object.keys(this).every(key =>
      this[key] !== null && this[key] !== undefined
    );
  }
};

class User extends Validatable(Timestamped(Serializable(class {}))) {
  constructor(name, email) {
    super();
    this.name = name;
    this.email = email;
  }
}

const user = new User('Alice', 'alice@example.com');
console.log(user.validate());
console.log(typeof user.serialize());
console.log(user.createdAt > 0);
```

Mixin factories take a Base class and return an extended class. Composing multiple mixins adds behaviors without deep inheritance.

## Key points

- Object.assign does shallow copy - nested objects are still shared references
- Mixins solve multiple inheritance without the diamond problem
- Object.create(null) is faster than {} for dictionaries and avoids prototype pollution
- Prefer composition over inheritance - mixins are a flexible composition mechanism

## Common follow-up questions

- What is the difference between Object.assign and the spread operator?
- How do mixins differ from multiple inheritance?
- What problem does Object.create(null) solve?

## Interview tips

- Object.create(null) use case - pure dictionaries without prototype pollution
- Mixin factories are a practical alternative to complex inheritance hierarchies
- Connect to 'prefer composition over inheritance' principle

## Concepts covered

Object.create, Mixins, Object composition, Prototypal Inheritance

Previous: [Async Iterators and for await...of](https://www.internhack.in/learn/interview/javascript-interview/js-async-iterators) · Next: [ES Modules vs CommonJS](https://www.internhack.in/learn/interview/javascript-interview/js-modules-advanced) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Promise Combinators](https://www.internhack.in/learn/interview/javascript-interview/js-promise-combinators)
- [Variables: var, let, const](https://www.internhack.in/learn/interview/javascript-interview/js-var-let-const)
- [Data Types & Type Checking](https://www.internhack.in/learn/interview/javascript-interview/js-data-types)
- [Operators & Short-Circuit Evaluation](https://www.internhack.in/learn/interview/javascript-interview/js-operators-short-circuit)
- [String Methods & Template Literals](https://www.internhack.in/learn/interview/javascript-interview/js-string-methods)
- [Array Methods (map, filter, reduce)](https://www.internhack.in/learn/interview/javascript-interview/js-array-methods)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
