What is the length of a function in js?

What is the length of a function in js?

Preface

Today I will tell you how to calculate the length of a function. I hope everyone can learn something from it and consolidate their basics.

Why

Why did I think of this knowledge point? Because last night, in a group, a classmate was discussing an interview question from ByteDance.

123['toString'].length + 123 = ?

To be honest, I couldn't answer this question at first. Actually, I knew that the interviewer wanted to test the toString method on the Number prototype, but I was stuck on the difficult question of what is the length of the toString function. That’s why I have this article today.

How much is it?

Number of parameters

Let's look at the following example

function fn1 () {}

function fn2 (name) {}

function fn3 (name, age) {}

console.log(fn1.length) // 0
console.log(fn2.length) // 1
console.log(fn3.length) // 2

It can be seen that the length is the same as the number of parameters of the function. But is this really the case? Continue reading

Default Parameters

What would be the length of the function if there were default parameters?

function fn1 (name) {}

function fn2 (name = '林三心') {}

function fn3 (name, age = 22) {}

function fn4 (name, age = 22, gender) {}

function fn5(name = '林三心', age, gender) { }

console.log(fn1.length) // 1
console.log(fn2.length) // 0
console.log(fn3.length) // 1
console.log(fn4.length) // 1
console.log(fn5.length) // 0

It shows that the length of a function is the number of parameters before the first one with a default value.

Rest parameters

There are also rest parameters in the function's formal parameters. How are they calculated if there are rest parameters?

function fn1(name, ...args) {}

console.log(fn1.length) // 1

It can be seen that the remaining parameters are not included in the calculation of length

Summarize

Before we conclude, let me first announce that the answer to 123['toString'].length + 123 = ? is 124.

To summarize, length is a property value of a function object, which refers to how many parameters must be passed into the function, that is, the number of formal parameters. The number of formal parameters does not include the remaining parameters, only the number of parameters before the first one with a default value.

This is the end of this article about the length of functions in js. For more information about the length of js functions, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • An article teaches you JS function inheritance
  • JavaScript Basics Series: Functions and Methods
  • Use of JavaScript sleep function
  • Exception handling of built-in functions json_encode and json_decode in php
  • How to make your JavaScript functions more elegant
  • Summary of 50+ Utility Functions in JavaScript
  • In-depth understanding of JavaScript callback functions
  • Three solutions for sub-functions accessing external variables in JavaScript
  • JavaScript functional programming basics

<<:  Detailed explanation of the solution to the problem of automatic disconnection of xshell remote connection

>>:  Detailed explanation of various practical uses of virtual device files in Linux system

Recommend

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

How to expand the disk partition for centos system

Problem/failure/scenario/requirement The hard dis...

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

How to implement blank space in Taobao with CSS3

Make a blank space for Taobao: When you shrink th...

How to implement remote automatic backup of MongoDB in Linux

Preface After reading the previous article about ...

HTML left and right layout example code

CSS: Copy code The code is as follows: html,body{ ...

Detailed explanation of using javascript to handle common events

Table of contents 1. Form events 2. Mouse events ...

HTML 5.1 learning: 14 new features and application examples

Preface As we all know, HTML5 belongs to the Worl...

CentOS 6.5 configuration ssh key-free login to execute pssh command explanation

1. Check and install pssh, yum list pssh 2. Becau...

How to use mysqldump for full and point-in-time backups

Mysqldump is used for logical backup in MySQL. Al...

Vue uses filters to format dates

This article example shares the specific code of ...

Vue implements the countdown component for second kills

This article shares the specific code of Vue to i...