Podcast
Questions and Answers
Describe JS Classes, Constructor Method, & instance properties / field declarations
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
Describe Class Declarations
Class declarations are defined using the class keyword followed by a capitalized name.
Describe Class Expressions
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.
Functions are hoisted, but classes are not.
Signup and view all the answers
Describe Calls Methods / Prototype Methods
Describe Calls Methods / Prototype Methods
Signup and view all the answers
Describe Generator Methods
Describe Generator Methods
Signup and view all the answers
Describe Static Methods & Properties
Describe Static Methods & Properties
Signup and view all the answers
Describe THIS with prototype & static methods.
Describe THIS with prototype & static methods.
Signup and view all the answers
Describe the usage of the 'Extends' keyword.
Describe the usage of the 'Extends' keyword.
Signup and view all the answers
Describe Super class calls with 'super'.
Describe Super class calls with 'super'.
Signup and view all the answers
Describe Mix-Ins (Abstract subclasses).
Describe Mix-Ins (Abstract subclasses).
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 accessthis
. - 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.
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.