Learn Before
Calling Object.prototype Methods
Avoid calling Object.prototype methods directly on objects, especially those that are not meant to be polymorphic. These methods may be defined differently in other objects descending from Object.prototype. Use modern JavaScript utilities such as valueOf() and toString() only if polymorphic, and replace deprecated methods like defineGetter() with Object.defineProperty(). Replace propertyIsEnumerable() with Object.getOwnPropertyDescriptor() and isPrototypeOf() with instanceof. If a semantically equivalent static method doesn't exist, safely call Object.prototype methods using .call().
const obj = { foo: 1, // You should not define such a method on your own object, // but you may not be able to prevent it from happening if // you are receiving the object from external input propertyIsEnumerable() { return false; } }; obj.propertyIsEnumerable("foo"); // false; unexpected result Object.prototype.propertyIsEnumerable.call(obj, "foo"); // true; expected result
0
1
Tags
Object-Oriented Programming (OOP) Languages
Object-Oriented Programming
Programming Language Paradigms
General programming languages
Computing Sciences