[英]Object javascript dont have build-in function hasOwnProperty
这是我的代码
console.log(typeof res.locals);
console.log(res.locals.hasOwnProperty('a'));
我的结果:
object
Unhandled rejection TypeError: res.locals.hasOwnProperty is not a function
注1:res是Express的响应对象;
我使用Express 4.13.3。有人知道这里有什么问题吗?
var a = Object.create(null);
var b = {};
a.hasOwnProperty('test');
b.hasOwnProperty('test');
我在这里发现错误Object.create(null)不能使用内置函数使Object javascript
res.locals
在Express中定义为没有原型的对象:
res.locals = res.locals || Object.create(null);
通过传递null
,该对象不会继承任何属性或方法,包括Object.prototype
上的Object.prototype
如hasOwnProperty
。
console.log(Object.getPrototypeOf(res.locals)); // null
console.log(Object.create(null) instanceof Object); // false
要将方法与res.locals
一起res.locals
,您必须通过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'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.