Sub topic 3
The arguments Object and Why Arrows Lack It
This chapter assumes you know arrow syntax, rest parameters, and how this differs between regular and arrow functions. Those topics were covered in the earlier chapters.
arguments, new, and prototype
Every regular function gets its own arguments object. It is array-like: it has a length and indexes, but no array methods. Arrow functions take arguments, like this, from the surrounding scope. Inside an arrow, use rest parameters instead.
Arrow functions also have no prototype property. Calling one with new throws a TypeError.
function regular() {
return arguments.length;
}
const arrow = (...args) => args.length;
function Person(name) {
this.name = name;
}
const ArrowPerson = (name) => {
this.name = name;
};
console.log(regular(1, 2, 3)); // 3
console.log(arrow(1, 2, 3)); // 3
console.log(new Person('Ada').name); // Ada
console.log(ArrowPerson.prototype); // undefined
try {
new ArrowPerson('Ada');
} catch (e) {
console.log(e instanceof TypeError); // true
}Methods, Callbacks, and Class Field Arrows
If you pull a class method off its instance and call it on its own, it loses this. Class bodies run in strict mode, so this becomes undefined. An arrow function stored in a class field captures the instance when the object is created, so it is safe to pass around as a callback.
class Button {
label = 'OK';
handleMethod() {
return this?.label;
}
handleArrow = () => this.label;
}
const { handleMethod, handleArrow } = new Button();
console.log(handleMethod()); // undefined
console.log(handleArrow()); // OKIIFEs and Block-Level Declarations
An IIFE (immediately invoked function expression) runs once to create a private scope:
const id = (function () {
let next = 0;
return () => ++next;
})();
console.log(id()); // 1
console.log(id()); // 2In strict mode, a function declaration inside a block is scoped to that block. Sloppy mode has legacy rules that make this behave differently, so don't rely on it. Use a const expression when the function should exist only conditionally.
Choosing the Right Function Form
| Need | Use |
|---|---|
| Top-level helper, called before its definition | Declaration |
| Object or prototype method | Method shorthand |
| Callback that needs the outer `this` | Arrow |
| Constructor | `class` or regular function |
| Recursion inside an expression | Named function expression |
| Handler passed around detached | Class field arrow |
Exercise
A logger is written as const log = () => console.log(arguments.length); at the top level of an ES module. Why does log(1, 2) fail, and how do you fix it?
Show answer
An arrow function has no arguments of its own. At module top level there is no outer arguments either, so referencing it throws a ReferenceError. Rewrite it as const log = (...args) => console.log(args.length);.
Exercise
Why can't const Point = (x, y) => { this.x = x; }; be used with new Point(1, 2)?
Show answer
Arrow functions are not constructors. They have no prototype and no internal [[Construct]] method, so new throws a TypeError. Use a class or a regular function instead.