The documentation says this object does not inherit from JavaScript Object
, however it does not show a reason why it does not inherit from it. So, Why does Node.js querystring.parse()
returns an object that does not inherit from the JavaScript Object
?

- 2,960
- 2
- 33
- 60
-
One thing I think of is maybe for performance if creating an object like this is relatively cheaper in Node.js – sçuçu Sep 17 '18 at 11:06
-
querystring is a object. If you change prototype of Object, it reelect in querystring. – Anoop Sep 17 '18 at 11:07
-
And another think is I cannot write a utility that depends on `Object` methods, like `Object.prototype.hasOwnPrototype`. By `call`ing on the querystring object it will error. For example, a utility checks if it is empty or not. – sçuçu Sep 17 '18 at 11:08
-
The doc says: "The object returned by the `querystring.parse()` method does not prototypically inherit from the JavaScript Object". It does not mean that it does not have a prototype. – Sujeet Jaiswal Sep 17 '18 at 11:10
-
@SujeetJaiswal you are right. I have edited accordingly. – sçuçu Sep 17 '18 at 11:10
-
See https://stackoverflow.com/q/18640776/1048572 – Bergi Sep 17 '18 at 13:52
-
just a quick work around for the qs.parse() issue, wrap it on an object.assign with an empty object literal. it will merge the objects and give you the proto needed: const body = Object.assign({}, qs.parse(event.body)); – NicoLA Feb 13 '19 at 17:22
1 Answers
The doc says:
The object returned by the querystring.parse() method does not prototypically inherit from the JavaScript Object
The statement does not imply that it has no prototype. But yes, in this case, it doesn't since the value of protoype
is null (shown below in code snippet).
To clarify why the statement doesn't imply it does not have a prototype, consider the value of obj:
var obj = Object.create(
Object.create(null, { test: { value: "test string" } })
);
console.log(Object.getPrototypeOf(obj) === Object.prototype); // false
console.log(Object.getPrototypeOf(obj) === undefined); // false
console.log(Object.getPrototypeOf(obj) === null); // false
It does not prototypically inherit from the JavaScript Object
, but it does not mean that it does not have a prototype. The prototype for obj
is actually an object, which has only one attribute of test
with value test string
Also, when the question was asked (before edit), it implied that the querystring
does not have the prototype, which is anyway not the case. (Question was edited again based on this input).
Execute the below code, you will understand better.
const querystring = require('querystring')
const parsed = querystring.parse('foo=bar&abc=xyz&abc=123')
console.log(Object.getPrototypeOf(querystring) === Object.prototype) // true
console.log(Object.getPrototypeOf(parsed) === Object.prototype) // false
console.log(Object.getPrototypeOf(parsed)) // null
Now, to answer why does the variable parsed
does not inherit from Object
is correctness and security, (based on inputs from comments):
so if you have something like:
const parsed = querystring.parse('__proto__=bar&toString=oops')
It would create an object with keys 'toString' & 'proto', and if those methods actually existed (assuming it inherits from Object), when used in the code, it would have throws error, as now toString is not a function but a string. Hence it does not inherits from Object.

- 1,071
- 7
- 12
-
1"*It does not mean that it has no prototype*" - actually yes, it does mean exactly that. – Bergi Sep 17 '18 at 13:56
-
Don't use `__proto__`, it is deprecated - because it does not work in exactly this situation here. Use `Object.getPrototypeOf` instead. – Bergi Sep 17 '18 at 13:57
-
Try with `querystring.parse('__proto__=bar&toString=ooops')`. It's a correctness (and security) issue, it has nothing to do with performance. – Bergi Sep 17 '18 at 13:57
-
@Bergi the object returned by `Object.create(Object.create(null, {test: {value: "test string"}}))` does not prototypically inherit from the JavaScript Object. but it does not mean that it does not have a prototype. Now in case querystring.parse it is undefined so you may say that it does not have a prototype, but the statement itself does not mean that it does not have a prototype. For the other two comments, I agree with you. – Sujeet Jaiswal Sep 17 '18 at 14:16
-
The statement means that "*The object returned by the `querystring.parse()`*" has no prototype. It does not say anything about the prototypes of the `querystring` module or the `querystring.parse` function. – Bergi Sep 17 '18 at 14:19
-
@Bergi I understand that it does the say anything about the prototypes of querystring module itself, but the question when asked stated that the module itself does not have the protoype. That's why I had made my answer to include that, but meanwhile the question was update. BTW, thanks for the inputs, it really helped me. If you have any other feedback, I would appreciate it very much. – Sujeet Jaiswal Sep 17 '18 at 14:47
-