Beginner javascript question: arrow function as optional argument

Kabocha Porter

I am having a hard time understanding the following class declaration: it seems that element is default to undefined, but I have no idea why

  1. is (x, y) in the element = (x,y) => undefined necessary; is (x,y) a abbreviated way of specifying array elements inside element object?
  2. use element(x, y) to access array elements inside element?
class Matrix {
  constructor(width, height, element = (x, y) => undefined) {
    this.width = width;
    this.height = height;
    this.content = [];

    for (let y = 0; y < height; y++) {
      for (let x = 0; x < width; x++) {
        this.content[y * width + x] = element(x, y);
      }
    }
  }

let matrix = new Matrix(2, 2, (x, y) => `value ${x},${y}`);

The code is from Chapter 6 of Eloquent JS booke

Zac Anger

element is being set to an arrow function expression that takes two arguments and returns undefined. This code would be equivalent:

constructor (width, height, element) {
  if (!element) element = (x, y) => undefined
}

And these two are mostly equivalent with the scope caveat (this in the version with the function keyword refers to that function):

(x, y) => undefined
function (x, y) { return undefined }

Either way it's written, element is a function that, by default (if no third parameter is passed when instantiating an instance of that class), will take two parameters x and y and return undefined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Beginner Question: Confusing Arrow Function Example

JavaScript: a small new beginner question about empty array, adding function argument to array

JavaScript optional destructuring argument in function

javascript: optional first argument in function

Arrow Function of JavaScript question - Multiple Arrow Function nesting

Javascript Concise Body Arrow Function Question

Command line argument question--Skipping optional function parameters?

Optional parameter in Arrow Function

Optional props arrow function

JavaScript: TypeError: work.calls is not iterable in function decorator (beginner question)

beginner question on recursion in javascript with BST

Beginner question for javascript button behavior

Javascript - Stuck in an if statement - Beginner Question

JavaScript - default values in destructured object passed to arrow function as an argument

Awk function with optional argument

distinguish if function argument optional

VBA Function - Argument Not Optional

Function with optional positional argument

R: Beginner Question: Recursive Function On Function

Beginner's question about function signature

Beginner Question: Wondering why the function needs to be repeated?

Need explanation on function input error (beginner question)

lapply with optional function argument and optional vector arguments

Detect optional function argument (scalar)

Detect optional function argument (array)

Pass function to optional argument in Python

Clojure optional argument to function call

A way to pass an optional argument to a function

Test function for checking an optional argument