I'm using a "hasOwnProperty" function to extend patibility but JSHint says that the Object.prototype.__proto__ is deprecated. There is a way to rewrite this function to avoid this warning and ensure the patibility?
var hasOwnProperty = function (obj, prop) {
var proto = obj.__proto__ || obj.constructor.prototype;
return (prop in obj) &&
(!(prop in proto) || proto[prop] !== obj[prop]);
};
I'm using a "hasOwnProperty" function to extend patibility but JSHint says that the Object.prototype.__proto__ is deprecated. There is a way to rewrite this function to avoid this warning and ensure the patibility?
var hasOwnProperty = function (obj, prop) {
var proto = obj.__proto__ || obj.constructor.prototype;
return (prop in obj) &&
(!(prop in proto) || proto[prop] !== obj[prop]);
};
Share
Improve this question
asked Mar 25, 2015 at 11:27
cardeolcardeol
2,23818 silver badges25 bronze badges
2
-
why not just
Object.prototype.hasOwnProperty.call(obj, prop)
? – Andy E Commented Mar 25, 2015 at 11:41 - 1 It's not deprecated. It's part of the current draft. – Knu Commented Mar 28, 2015 at 16:56
2 Answers
Reset to default 11The "correct" way to do what you're trying to do is to use the Object.getPrototypeOf
function:
var proto = Object.getPrototypeOf(obj);
That's not supported in Internet Explorer 8 and below though so if you need to support old environments you could extend your test to include a check for that, and fall back to __proto__
where necessary.
That will obviously not avoid the JSHint warning though so you'll probably still want to set the proto
option to turn it off.
If you wanted to avoid rewriting the code you could add the following to the top of your file. It's one of the "relaxing" options in JSHint you can use to reduce the number of warnings you get:
/* jshint proto: true */