📃
Julianne's Knowledge Base
  • README
  • Home
    • Computer Science and Programming
      • Data Structures
        • Array
        • Binary Heap
        • Binary Tree
        • Deque
        • Dynamic Array
        • Graph
        • Hash Table
        • Linked List
        • Queue
        • Stack
      • Databases
        • Database Normalization
      • Networking
        • IP Protocol
        • Network Devices
        • OSI Model and TCP/IP
        • Ethernet LAN Switching
        • IPv4 Addressing
        • Sockets
      • Operating Systems
        • UNIX Operating Systems
          • Fundamentals
            • Virtualization
              • CPU Virtualization
              • Processes
            • Processes
          • xv6
      • Software Development
        • General Tips
        • My Goals as a Software Developer
        • Programming Languages
          • C
            • Memory Management in C
          • C++
            • I/O in C++
            • Iterators
            • Memory Management in C++
          • Javascript/TypeScript
            • Inheritance
            • React
            • Useful Libraries
          • Python
        • Tools
          • GDB
          • Git
    • Cooking
      • Diet and Nutrition
      • Recipes
    • Languages
      • Japanese
        • Grammar
        • Numbers and Counting
    • Productivity
      • Getting Things Done (GTD)
      • My GTD Trigger List
      • My Personal Knowledge Management System
      • Obsidian
        • Plugins
        • Using Obsidian
Powered by GitBook
On this page
  • Basics
  • Constructors
  • Prototypes
  • Links
  1. Home
  2. Computer Science and Programming
  3. Software Development
  4. Programming Languages
  5. Javascript/TypeScript

Inheritance

Basics

  • When you want to implement inheritance, you attach properties and methods to the prototype

  • Every JS function has a prototype property

    • The prototype property is empty by default

    • The prototype property is not enumerable

  • Every JS object has a prototype attribute

    • Points to the object's parent

    • Set automatically when the object is created

  • All JS objects inherit the following properties and methods from Object.prototype:

    • constructor

    • hasOwnProperty()

    • isPrototypeOf()

    • propertyIsEnumerable()

    • toLocaleString()

    • toString()

    • valueOf()

Constructors

  • Functions used for initializing new objects

  • Called using the new keyword

  • All objects inherit a constructor property that points to the constructor of the object

  • If an object is created with an object literal, it inherits from Object.prototype; if an object is created from a constructor, it inherits from the constructor

Prototypes

  • Every object's prototype is either null or references another object

  • The prototype property is internal and hidden, though there's a few ways to access it

    • You can use myObject.__proto__

      • This is a historical getter/setter

      • Has been replaced with Object.getPrototypeOf and Object.setPrototypeOf

  • Prototypes are only used for reading

    • All write/delete operations operate directly on the object

    • Accessor functions are an exception, since calling a setter is technically writing

  • The this keyword is not affected by prototypes

Links

PreviousJavascript/TypeScriptNextReact

Last updated 4 years ago

Currently, the recommended way of setting the prototype of an object is

Object.create
JavaScript is Sexy - Javascript Prototype in Plain Language
JavaScript.info - Prototypical Inheritance
The Odin Project - Objects and Object Constructors