question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule suggestion - Prevent using built-in prototype properties on an object

See original GitHub issue

Prevent 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:closed
  • Created 8 years ago
  • Comments:43 (43 by maintainers)

github_iconTop GitHub Comments

1reaction
ilyavolodincommented, May 4, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found