JavaScript Class Definition

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What syntax do we use to define classes in JavaScript?

class ClassName { constructor (params) { } methodName() { } }

What are the two types of objects in JavaScript?

  • Functional (correct)
  • Classical (correct)
  • Dynamic
  • Static

How must you refer to other methods in the class?

this.

How can we declare public fields?

<p>Class rectangle { height = 0; width; constructor(height, width) { this.height = height; this.width = width; }}</p> Signup and view all the answers

How can we declare private fields?

<p>Class rectangle { #height = 0; this.#height = height; }</p> Signup and view all the answers

What are first class functions?

<p>Functions that are treated like variables of type function.</p> Signup and view all the answers

What is a function in JavaScript?

<p>A function is an object of type function created with an expression starting with the keyword function.</p> Signup and view all the answers

What are the properties of a function?

<p>Functions have properties like name and toString().</p> Signup and view all the answers

What does the call method do in a function?

<p>Invokes the underlying executable code associated with this function object.</p> Signup and view all the answers

What does () do in a function?

<p>It calls the function's call() method which executes the underlying code.</p> Signup and view all the answers

What is the difference between a function and a function object?

<p>A function is executable code, while a function object is a JS object with executable code that can be invoked.</p> Signup and view all the answers

Regular JavaScript objects can be invoked.

<p>False (B)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Class Definition in JavaScript

  • Classes are defined using the class ClassName { ... } syntax.
  • Include a constructor(params) method for initial object properties.
  • Other methods can be defined within the class using the methodName() { ... } structure.

Types of Objects

  • Functional Objects:

    • Fundamental since the inception of JavaScript.
    • Behave differently than objects in Java or C++.
  • Classical Objects:

    • Introduced in 2015, they provide a more approachable syntax over functional objects.

Method References

  • Other methods within the class are referred to using the this prefix to maintain context.

Public Fields

  • Declare public fields directly in the class body:
    • Example:
      class Rectangle {
          height = 0;
          width;
      
          constructor(height, width) {
              this.height = height;
              this.width = width;
          }
      }
      

Private Fields

  • Private fields use a # prefix:
    • Example:
      class Rectangle {
          #height = 0;
      
          constructor(height) {
              this.#height = height;
          }
      }
      
  • Only accessible within the class body.

First-Class Functions

  • Functions are treated as first-class citizens in JavaScript:
    • Can be passed as parameters.
    • Can be stored in variables.
    • Can be anonymous (no name).

Function Characteristics

  • A function in JS is an object of type function, created using the function keyword.
  • Must have parameters and a body wrapped in curly brackets, regardless of size.
  • Can have multiple or no parameters and can be named or anonymous.

Function Properties

  • Each function object has properties like name and toString().
    • Example:
      const greeting = function() {
          console.log('hello');
      };
      console.log(greeting.name); // Accesses the name property
      console.log(greeting.toString()); // Displays the function body as a string.
      

Function Call Method

  • The call method invokes the code associated with a function object directly.

Function Invocation Syntax

  • Using parentheses () on a function object calls its call() method, executing the underlying code.

Understanding Functions

  • Distinction between the function (executable code) and the function object (a callable JS object).
  • Functions can be invoked by name or using the call() method.

Special Status of Functions

  • Functions can be invoked, unlike regular JavaScript objects.
  • Regular JS objects can't execute code, which makes functions unique and special.

Studying That Suits You

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

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser