Skip to content

Instantly share code, notes, and snippets.

@BolajiAyodeji
Created March 15, 2019 15:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BolajiAyodeji/889006fe1c6f23fd7b99908df91975b6 to your computer and use it in GitHub Desktop.
Save BolajiAyodeji/889006fe1c6f23fd7b99908df91975b6 to your computer and use it in GitHub Desktop.
JavaScript Prototypes Cheat Sheet.
// Every object (except the root object) has a prototype (parent).
// To get the prototype of an object:
Object.getPrototypeOf(obj);
// In Chrome, you can inspect "__proto__" property. But you should
// not use that in the code.
// To get the attributes of a property:
Object.getOwnPropertyDescriptor(obj, 'propertyName');
// To set the attributes for a property:
Object.defineProperty(obj, 'propertyName', {
configurable: false, // cannot be deleted
writable: false,
enumerable: false
});
// Constructors have a "prototype" property. It returns the object
// that will be used as the prototype for objects created by the constructor.
Object.prototype === Object.getPrototypeOf({})
Array.prototype === Object.getPrototypeOf([])
// All objects created with the same constructor will have the same prototype.
// A single instance of this prototype will be stored in the memory.
const x = {};
const y = {};
Object.getPrototypeOf(x) === Object.getPrototypeOf(y); // returns true
// Any changes to the prototype will be immediately visible to all objects
// referencing this prototype.
// When dealing with large number of objects, it's better to put their
// methods on their prototype. This way, a single instance of the methods
// will be in the memory.
Circle.prototype.draw = function() {}
// To get the own/instance properties:
Object.keys(obj);
// To get all the properties (own + prototype):
for (let key in obj) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment