The lodash isPlainObject utility function for checking if a value is a plain object created by Object constructor or with null prototype
npx @tessl/cli install tessl/npm-lodash--isplainobject@4.0.00
# lodash.isplainobject
1
2
lodash.isplainobject provides the lodash isPlainObject utility function as a standalone module for checking if a value is a plain object (an object created by the Object constructor or one with a [[Prototype]] of null). The function performs sophisticated type checking by examining the object's prototype chain and constructor function, handling edge cases like host objects in older Internet Explorer versions.
3
4
## Package Information
5
6
- **Package Name**: lodash.isplainobject
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.isplainobject`
10
11
## Core Imports
12
13
```javascript
14
var isPlainObject = require('lodash.isplainobject');
15
```
16
17
For ES modules:
18
19
```javascript
20
import isPlainObject from 'lodash.isplainobject';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var isPlainObject = require('lodash.isplainobject');
27
28
// Check plain objects
29
console.log(isPlainObject({})); // => true
30
console.log(isPlainObject({ x: 0, y: 0 })); // => true
31
console.log(isPlainObject(Object.create(null))); // => true
32
33
// Check non-plain objects
34
console.log(isPlainObject([1, 2, 3])); // => false
35
console.log(isPlainObject(function() {})); // => false
36
console.log(isPlainObject(null)); // => false
37
console.log(isPlainObject(new Date())); // => false
38
39
// Check constructor function instances
40
function Foo() {
41
this.a = 1;
42
}
43
console.log(isPlainObject(new Foo())); // => false
44
```
45
46
## Capabilities
47
48
### Plain Object Detection
49
50
Checks if a value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null.
51
52
```javascript { .api }
53
/**
54
* Checks if value is a plain object, that is, an object created by the
55
* Object constructor or one with a [[Prototype]] of null.
56
*
57
* @param {*} value The value to check.
58
* @returns {boolean} Returns true if value is a plain object, else false.
59
*/
60
function isPlainObject(value);
61
```
62
63
**Parameters:**
64
- `value` (*): The value to check
65
66
**Returns:**
67
- `boolean`: Returns `true` if `value` is a plain object, else `false`
68
69
**Behavior:**
70
- Returns `true` for objects created via object literals (`{}`)
71
- Returns `true` for `Object.create(null)`
72
- Returns `true` for objects created via `new Object()`
73
- Returns `false` for arrays, functions, DOM elements
74
- Returns `false` for instances of custom constructor functions
75
- Returns `false` for null, undefined, primitives
76
- Handles edge cases like host objects in older IE versions
77
78
**Usage Examples:**
79
80
```javascript
81
var isPlainObject = require('lodash.isplainobject');
82
83
// Plain objects return true
84
isPlainObject({}); // => true
85
isPlainObject({ x: 0, y: 0 }); // => true
86
isPlainObject(Object.create(null)); // => true
87
isPlainObject(new Object()); // => true
88
89
// Non-plain objects return false
90
isPlainObject([1, 2, 3]); // => false (arrays)
91
isPlainObject(function() {}); // => false (functions)
92
isPlainObject(null); // => false (null)
93
isPlainObject(undefined); // => false (undefined)
94
isPlainObject(42); // => false (numbers)
95
isPlainObject('hello'); // => false (strings)
96
isPlainObject(true); // => false (booleans)
97
isPlainObject(new Date()); // => false (Date instances)
98
isPlainObject(/regex/); // => false (RegExp instances)
99
100
// Constructor function instances return false
101
function CustomConstructor() {
102
this.prop = 'value';
103
}
104
isPlainObject(new CustomConstructor()); // => false
105
106
// Class instances return false
107
class MyClass {
108
constructor() {
109
this.prop = 'value';
110
}
111
}
112
isPlainObject(new MyClass()); // => false
113
```