Skip to content

Chapter 6 of 8

Objects, Classes, and Built-in APIs

Even though ES6 introduced the class keyword, JavaScript's object system is built on prototypes. The prototype chain is how JavaScript looks up a property: if it is not found on an object itself, the engine looks on the object's prototype, then the prototype's prototype, and so on. This chain is what powers inheritance and is the reason methods can be shared across many objects. A common mistake is to confuse JavaScript prototypes with classical classes in languages like Java or C#—they look similar but differ in how inheritance actually works under the hood.

The ES6 class syntax is essentially syntactic sugar on top of prototypes. A class is defined with class MyClass { constructor(props) { this.props = props; } method() {} }. Class inheritance uses extends to create a child class and super() inside the child's constructor to call the parent constructor, ensuring the parent's initialization runs first. The this keyword refers to the execution context object: it points to window in the global scope, to the calling object inside regular methods, and is inherited from the surrounding lexical scope inside arrow functions.

JavaScript ships with many built-in objects for everyday tasks. The Math object offers utilities such as Math.random(), Math.floor(), and constants like Math.PI, with no constructor of its own. The Date object represents a moment in time and is created with new Date() for the current instant or new Date('2023-01-01') for a specific one; methods like getFullYear() and setHours() read and update parts of the date. JSON (JavaScript Object Notation) is a plain-text format for data interchange, written like {"name": "JS"}, and is converted to and from JavaScript objects with JSON.parse() and JSON.stringify(). Array methods such as map(), filter(), and reduce() provide functional-style transformations: map() builds a new array by applying a function to each element, filter() keeps only elements that pass a boolean test, and reduce() folds an array down to a single accumulated value.

All chapters
  1. 1JavaScript Foundations
  2. 2Variables, Types, and Operators
  3. 3Control Flow and Functions
  4. 4Data Structures and ES6 Syntax
  5. 5Scope, Hoisting, and Closures
  6. 6Objects, Classes, and Built-in APIs
  7. 7Asynchronous JavaScript
  8. 8Browser Interaction and Best Practices

Drill it

Reading is not remembering. These come from the Javascript Fundamentals deck:

Q

What is JavaScript?

JavaScript is a high-level, interpreted programming language primarily used for web development to make web pages interactive. It is the scripting language of t...

Q

Who created JavaScript and in what year?

JavaScript was created by Brendan Eich in 1995 while working at Netscape. It was initially called Mocha, then LiveScript, before being renamed JavaScript.

Q

What is ECMAScript?

ECMAScript (ES) is the standardized scripting language specification upon which JavaScript is based. The latest stable version is ES2023, with JavaScript implem...

Q

How do you include JavaScript in an HTML file?

Use the <script> tag in the HTML, either inline with code between tags or externally via <script src="script.js"></script>. Place it before th...