node.js - How to check for Object key in javascript for object not inheriting from Object.prototype -
in node 6.x querystring module parses values object not extend object.prototype (although have no idea why change made).
given that, expected way check query parameters not contain value, i.e. called express or koa localhost:8080/some/path?keyonly , want check presence of this.query.keyonly? expected this.query.hasownproperty('keyonly') doesn't work. what's alternative?
for background behind change, see original pr here.
in general, should not rely on foo.hasownproperty() since hasownproperty reassigned else (hence reason querystring change -- imagine calling web service http://example.org/foo?hasownproperty=hahacrashed).
as far alternatives, check if property defined, example:
var qs = querystring.parse('foo=bar'); if (qs.foo !== undefined) { // ... } you like:
var qs = querystring.parse('foo=bar'); if (object.hasownproperty.call(qs, 'foo')) { // ... } but isn't efficient because hasownproperty() performs additional (and unnecesary in case of querystring now) checks , uses .call() less efficient
Comments
Post a Comment