You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+62-6
Original file line number
Diff line number
Diff line change
@@ -8076,14 +8076,14 @@
8076
8076
8077
8077
434. ### What is debouncing?
8078
8078
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.
8080
8080
8081
8081
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.
8082
8082
8083
8083
```js
8084
8084
functiondebounce(func, timeout=500) {
8085
8085
let timer;
8086
-
return (...args) => {
8086
+
returnfunction(...args) {
8087
8087
clearTimeout(timer);
8088
8088
timer =setTimeout(() => {
8089
8089
func.apply(this, args);
@@ -8096,7 +8096,7 @@
8096
8096
constprocessChange=debounce(() =>fetchResults());
8097
8097
```
8098
8098
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.
8100
8100
8101
8101
**Input:**
8102
8102
@@ -8120,7 +8120,7 @@
8120
8120
8121
8121
435. ### What is throttling?
8122
8122
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.
8124
8124
8125
8125
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.
8126
8126
@@ -8784,8 +8784,6 @@ The execution context is created when a function is called. The function's code
8784
8784
8785
8785
**[⬆ Back to Top](#table-of-contents)**
8786
8786
8787
-
<!-- QUESTIONS_END -->
8788
-
8789
8787
465. ### How to detect system dark mode in javascript?
8790
8788
8791
8789
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
8811
8809
8812
8810
**[⬆ Back to Top](#table-of-contents)**
8813
8811
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
+
functionmultiply(x, y) {
8825
+
return x * y;
8826
+
}
8827
+
8828
+
functionsum(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
+
functionsum(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
+
functionsum(a, b, ...moreArgs) {
8848
+
let total = a + b;
8849
+
for (constargof 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
+
functionfunc([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.
0 commit comments