[英]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.