1. Do not call Object.prototype methods directly

Almost all objects in JavaScript are instances of [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) and therefore inherit methods from Object.prototype .

Some examples of these methods are hasOwnProperty , propertyIsEnumerable , and isPrototypeOf .

There are a number of subtle bugs which can occur when you call these methods directly on an object.

Null object

It’s possible to create a null object which does not inherit from Object.prototype using the following constructor.

const obj = Object.create(null)

This is typically done when we want to solely use the object as a Map.

Calling an Object.prototype method on obj will crash our program.

obj.hasOwnProperty("name")
//Uncaught TypeError: obj.hasOwnProperty is not a function

#web-development #javascript-tips #software-development #javascript #programming

3 Tips for Better Objects in JavaScript
1.35 GEEK