https://github.com/bttmly/is-pojo
check if something is a POJO (Plain Old Javascript Object)
https://github.com/bttmly/is-pojo
Last synced: 17 days ago
JSON representation
check if something is a POJO (Plain Old Javascript Object)
- Host: GitHub
- URL: https://github.com/bttmly/is-pojo
- Owner: bttmly
- Created: 2014-10-13T18:43:42.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-04-05T07:27:08.000Z (about 10 years ago)
- Last Synced: 2025-04-04T18:22:01.798Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 211 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# is-pojo [](https://travis-ci.org/nickb1080/is-pojo) [](http://badge.fury.io/js/is-pojo)
### Installation
`npm install is-pojo`### Usage
```js
var isPojo = require("is-pojo");
isPojo({}) // true// anything besides an absolutely plain object will return false.
// here are examples from the tests:
function Foo () {}
function Bar () {}
Bar.prototype.constructor = Object;isPojo(function () {}) // false
isPojo([]) // false
isPojo(new Date()) // false
isPojo(true); // false
isPojo("abc"); // false
isPojo(123); // false
isPojo(new RegExp()); // false
isPojo(null); // false
isPojo(undefined); // false
isPojo(Object.create({})); // false
isPojo(new Foo()); // false
isPojo(new Bar()); // false
isPojo({constructor: Foo}); // true
```### Why?
The [other module on npm](https://www.npmjs.org/package/is-plain-object) that I found to do this checks an object's `constructor` property against `Object`. This approach is error-prone since there's nothing special about the `constructor` property; you can set it willy-nilly (see example above).In contrast, an object's _actual_ prototype (obtained with `Object.getPrototypeOf`) is the real and only definition of an object's nature -- even in the possible case that its prototype has been changed after creation (either though the spec compliant `Object.setPrototypeOf` or by changing the commonly implemented `__proto__ ` property).