Skip to content

Commit 11275f5

Browse files
committed
Add function length question
1 parent beef0ed commit 11275f5

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

README.md

+62-6
Original file line numberDiff line numberDiff line change
@@ -8076,14 +8076,14 @@
80768076
80778077
434. ### What is debouncing?
80788078
8079-
Debouncing is a programming pattern that allows delaying execution of some piece of code until a specified time to avoid unnecessary _CPU cycles, API calls and improve performance_. The debounce function make sure that your code is only triggered once per user input. The common usecases are Search box suggestions, text-field auto-saves, and eliminating double-button clicks.
8079+
Debouncing is a programming pattern that allows delaying execution of some piece of code until a specified time to avoid unnecessary _CPU cycles and API calls_. This in turn enhance the web page performance. The debounce function make sure that your code is only triggered once per user input. The common usecases are Search box suggestions, text-field auto-saves, and eliminating double-button clicks.
80808080
80818081
Let's say you want to show suggestions for a search query, but only after a visitor has finished typing it. So here you write a debounce function where the user keeps writing the characters with in 500ms then previous timer cleared out using `clearTimeout` and reschedule API call/DB query for a new time—300 ms in the future.
80828082
80838083
```js
80848084
function debounce(func, timeout = 500) {
80858085
let timer;
8086-
return (...args) => {
8086+
return function (...args) {
80878087
clearTimeout(timer);
80888088
timer = setTimeout(() => {
80898089
func.apply(this, args);
@@ -8096,7 +8096,7 @@
80968096
const processChange = debounce(() => fetchResults());
80978097
```
80988098
8099-
The _debounce()_ function can be used on input, button and window events
8099+
The _debounce()_ function can be used on input, button and window events.
81008100
81018101
**Input:**
81028102
@@ -8120,7 +8120,7 @@
81208120
81218121
435. ### What is throttling?
81228122
8123-
Throttling is a technique used to limit the execution of an event handler function, even when this event triggers continuously due to user actions. The common use cases are browser resizing, window scrolling etc.
8123+
Throttling is a technique used to limit the execution of an event handler function in a given period of time, even when this event triggers continuously due to user actions. The common use cases are browser resizing, window scrolling, mouse movements etc.
81248124
81258125
The below example creates a throttle function to reduce the number of events for each pixel change and trigger scroll event for each 100ms except for the first event.
81268126
@@ -8784,8 +8784,6 @@ The execution context is created when a function is called. The function's code
87848784
87858785
**[⬆ Back to Top](#table-of-contents)**
87868786
8787-
<!-- QUESTIONS_END -->
8788-
87898787
465. ### How to detect system dark mode in javascript?
87908788
87918789
The combination of `Window.matchMedia()` utility method along with media query is used to check if the user has selected a dark color scheme in their operating system settings or not. The CSS media query `prefers-color-scheme` needs to be passed to identify system color theme.
@@ -8811,6 +8809,64 @@ The execution context is created when a function is called. The function's code
88118809
88128810
**[⬆ Back to Top](#table-of-contents)**
88138811
8812+
466. ### What is the purpose of requestAnimationFrame method?
8813+
8814+
**[⬆ Back to Top](#table-of-contents)**
8815+
8816+
467. ### What is the difference between substring and substr methods?
8817+
8818+
**[⬆ Back to Top](#table-of-contents)**
8819+
8820+
468. ### How to find the number of parameters expected by a function?
8821+
The function's object has a **length** property which tells you how many formal parameters expected by a function. This is a static value defined by the function, not the number of arguments the function is called with(__arguments.length__). The basic usage of length propery is,
8822+
8823+
```javascript
8824+
function multiply(x, y) {
8825+
return x * y;
8826+
}
8827+
8828+
function sum(a, b, c) {
8829+
return a + b +c;
8830+
}
8831+
8832+
console.log(multiply.length); //2
8833+
console.log(sum.length); //3
8834+
```
8835+
8836+
But there are few important rules which needs to be noted while using length property.
8837+
8838+
1. **Default values:** Only the parameters which exists before a default value are considered.
8839+
```javascript
8840+
function sum(a, b=2, c=3) {
8841+
return a + b + c;
8842+
}
8843+
console.log(sum.length); // 1
8844+
```
8845+
2. **Rest params:** The rest parameters are excluded with in length property.
8846+
```javascript
8847+
function sum(a, b, ...moreArgs) {
8848+
let total = a + b;
8849+
for (const arg of moreArgs) {
8850+
total += arg;
8851+
}
8852+
return total;
8853+
}
8854+
console.log(sum.length); // 2
8855+
```
8856+
3. **Destructuring patterns:** Each destructuring pattern counted as a single parameter.
8857+
```javascript
8858+
function func([a, b], {x, y}){
8859+
console.log(a+b, x, y);
8860+
}
8861+
8862+
console.log(func.length); // 2
8863+
```
8864+
8865+
**Note:** The Function constructor is itself a function object and it has a length property of 1.
8866+
8867+
**[⬆ Back to Top](#table-of-contents)**
8868+
8869+
88148870
<!-- QUESTIONS_END -->
88158871
88168872
### Coding Exercise

0 commit comments

Comments
 (0)