简体   繁体   English

对象javascript没有内置函数hasOwnProperty

[英]Object javascript dont have build-in function hasOwnProperty

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; 注1:res是Express的响应对象;

I use Express 4.13.3 .Anyone know what problem here ? 我使用Express 4.13.3。有人知道这里有什么问题吗?

NOTE : 注意 :

  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 我在这里发现错误Object.create(null)不能使用内置函数使Object javascript

res.locals is defined in Express as an object without a prototype: res.locals 在Express中定义为没有原型的对象:

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 . 通过传递null ,该对象不会继承任何属性或方法,包括Object.prototype上的Object.prototypehasOwnProperty

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 : 要将方法与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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM