JavaScript: Special object

Last update: 10 Jan 23:20
0
Students

JavaScript objects allow you to access their own properties. When accessing a non-existing property, undefined is returned:

const obj = {
  key: 'value',
  key2: {
    key3: 'value3',
  },
};

obj.key2 // { key3: 'value3' }
obj.key2.key1 // undefined
obj.key2.key1.key0 // Uncaught TypeError: Cannot read property 'key0' of undefined

In this challenge, we'll implement a special object that enables us to access nonexistent properties without getting an error.

object.js

Implement and export as default a function that takes an object and allows you to access its properties by any name. When attempting to access a property that doesn't exist, neither exceptions nor the undefined should be thrown. The function should return a Proxy object.

import createObject from './object.js';

const obj = createObject({
  key: 'value',
  key2: {
    key3: 'value3',
  },
});

obj.key2 // { key3: 'value3' }

// the code keeps executing:
obj.key2.key1
obj.key2.key1.key0
obj.obj.obj

Tips

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests