Javascript Classes Flashcards
11 Questions
100 Views

Javascript Classes Flashcards

Created by
@SlickBinary8749

Questions and Answers

Describe JS Classes, Constructor Method, & instance properties / field declarations

Classes are templates for creating objects and encapsulate data with code, built on prototypes. The constructor is a special method for creating and initializing objects. Instance properties must be defined inside class methods, while field declarations are made outside of methods.

Describe Class Declarations

Class declarations are defined using the class keyword followed by a capitalized name.

Describe Class Expressions

Class expressions can be named or unnamed and local names are specific to the class's body.

Functions are hoisted, but classes are not.

<p>True</p> Signup and view all the answers

Describe Calls Methods / Prototype Methods

<p>Call methods or prototype methods are functions on a class accessed via the class to perform actions.</p> Signup and view all the answers

Describe Generator Methods

<p>Generator methods use 'function*' to return a generator function and can be exited and re-entered with context saved.</p> Signup and view all the answers

Describe Static Methods & Properties

<p>Static methods and properties belong to the class itself and cannot be called on class instances.</p> Signup and view all the answers

Describe THIS with prototype & static methods.

<p>When a static or prototype method is called without a value for THIS, it will be undefined.</p> Signup and view all the answers

Describe the usage of the 'Extends' keyword.

<p>The 'Extends' keyword creates a subclass of another class and requires calling super() if the subclass has a constructor.</p> Signup and view all the answers

Describe Super class calls with 'super'.

<p>The 'super' keyword is used to call methods on the superclass, enabling functionality to be passed to child classes.</p> Signup and view all the answers

Describe Mix-Ins (Abstract subclasses).

<p>Mix-ins are class templates containing methods that other classes can use without inheritance.</p> Signup and view all the answers

Study Notes

JavaScript Classes Overview

  • JavaScript classes are templates for creating objects that encapsulate data and provide methods to manipulate that data.
  • Built on the concept of prototypes with modern syntax and semantics, classes run in Strict Mode and cannot be redefined at runtime.

Constructor Method

  • The constructor is a special method that initializes objects created from a class and can call parent constructors using the super keyword.
  • Only one constructor can be defined per class.

Instance Properties & Field Declarations

  • Instance properties can only be defined within class methods, while field declarations can be directly within the class body.
  • Field declarations include explicit definitions and can be public or private, e.g., #height = 10.

Class Declarations

  • A class is defined using the class keyword followed by a capitalized name, e.g., class Rectangle { ... }.

Class Expressions

  • Classes can be defined through expressions, which can be named or unnamed.
  • Named class expressions have names that are local to the class's body, while unnamed class expressions can be directly assigned to variables.

Hoisting Differences

  • Function declarations are hoisted in JavaScript, allowing them to be called before their definition.
  • Class declarations are not hoisted and must be declared before use.

Call Methods / Prototype Methods

  • Call methods are functions defined on the class that perform actions and can be accessed from instances of the class.
  • Shorthand syntax in object literals allows omitting the function keyword, making the code more concise.

Generator Methods

  • Generator methods are defined with function* and return a generator function that can pause and resume execution while maintaining context.
  • Indicated with an asterisk before the method name, they help manage asynchronous workflows.

Static Methods & Properties

  • Static methods and properties are associated with the class itself, not with instances, and are often used for utility functions or shared configurations.
  • They are accessed directly from the class rather than on class instances.

Context (this) in Methods

  • When static or prototype methods are invoked without a specific context, this will be undefined.
  • The class body is always executed in strict mode, making this handling consistent.

Using the extends Keyword

  • The extends keyword is used to create subclasses by extending a parent class.
  • If a subclass has a constructor, it must call super() before it can access this.
  • ES6 classes can extend traditional function-based classes.

Super Class Calls

  • The super keyword allows calling methods from a parent class, facilitating straightforward inheritance and method overriding.
  • This access can return data back to the child class for further manipulation.

Mix-Ins (Abstract Subclasses)

  • Mix-ins allow classes to utilize methods from other classes without requiring inheritance, addressing the limitation of single inheritance in ECMAScript.
  • They provide a way to elegantly share functionality across different classes.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

This quiz focuses on the concepts of Javascript classes, including their structure, the constructor method, and instance properties. Test your knowledge on how classes serve as templates for creating objects and encapsulating data. Perfect for those diving into modern Javascript programming.

More Quizzes Like This

JavaScript Flashcards
95 questions

JavaScript Flashcards

JubilantUvarovite avatar
JubilantUvarovite
Basic JavaScript Functions Quiz
6 questions

Basic JavaScript Functions Quiz

SustainableAntigorite1088 avatar
SustainableAntigorite1088
JavaScript Class Definition
12 questions
Use Quizgecko on...
Browser
Browser