Rule suggestion - Prevent using built-in prototype properties on an object
See original GitHub issuePrevent use of built-in prototype properties on an object as these won’t work on objects without a prototype, eg. object created with Object.create(null)
. You never know when you’ll get an object like that, so better be safe than sorry. This rule would have prevented #2688.
Should prevent the use of these:
Object.getOwnPropertyNames(Object.prototype);
//=> ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__", "__lookupGetter__", "__defineSetter__", "__lookupSetter__", "__proto__"]
Example of misuse:
var obj = {};
obj.foo = true;
obj.hasOwnProperty('foo');
//=> true
Which will fail here:
var obj = Object.create(null);
obj.foo = true;
obj.hasOwnProperty('foo');
//=> 'Uncaught TypeError: obj.hasOwnProperty is not a function'
Instead this should be used:
var obj = Object.create(null);
obj.foo = true;
Object.prototype.hasOwnProperty.call(obj, 'foo');
//=> 'foo'
<bountysource-plugin>
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 8 years ago
- Comments:43 (43 by maintainers)
Top Results From Across the Web
Prototype methods, objects without __proto__
Setting or reading the prototype with obj.__proto__ is considered outdated and somewhat deprecated (moved to the so-called “Annex B” of the ...
Read more >Object.preventExtensions() - JavaScript - MDN Web Docs
Object.preventExtensions() only prevents addition of own properties. Properties can still be added to the object prototype. This method makes ...
Read more >How does JavaScript .prototype work? - Stack Overflow
an own property, checkable via obj.hasOwnProperty('propName') ), the runtime looks for a property with that name on the object referenced by the [[Prototype]] ......
Read more >no-prototype-builtins - ESLint - Pluggable JavaScript Linter
prototype . This rule prevents calling some Object. prototype methods directly from an object. Additionally, objects can have properties that shadow the ......
Read more >Chapter 17. Objects and Inheritance - Exploring JS
Layer 2: The Prototype Relationship Between Objects · Inheritance. obj inherits the property describe ; you can access it as if the object...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes: http://eslint.org/blog/2016/05/eslint-v2.11.0-released#new-rules
I’ll support this. It’s a pretty important rule rule that’s hard for new users to understand without learning more about prototypical inheritance. So I think it can save people from creating a lot of errors.