Here my code
console.log(typeof res.locals);
console.log(res.locals.hasOwnProperty('a'));
My result :
object
Unhandled rejection TypeError: res.locals.hasOwnProperty is not a function
Note 1: res is response Object by Express;
I use Express 4.13.3 .Anyone know what problem here ?
var a = Object.create(null);
var b = {};
a.hasOwnProperty('test');
b.hasOwnProperty('test');
I find bug here Object.create(null) dont make Object javascript with buildin function
res.locals
is defined in Express as an object without a prototype:
res.locals = res.locals || Object.create(null);
By passing null
, the object doesn't inherit any properties or methods, including those on Object.prototype
like hasOwnProperty
.
console.log(Object.getPrototypeOf(res.locals)); // null
console.log(Object.create(null) instanceof Object); // false
To use the method with res.locals
, you'll have to access it through the Object.prototype
:
console.log(Object.prototype.hasOwnProperty.call(res.locals, 'a'));
// or store it
var hasOwnProperty = Object.prototype.hasOwnProperty;
console.log(hasOwnProperty.call(res.locals, 'a'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.