最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSHINT: The __proto__ property is deprecated - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 11

The "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 */
发布评论

评论列表(0)

  1. 暂无评论