parent
e9651c6afe
commit
6713b059e1
4 changed files with 72 additions and 0 deletions
@ -0,0 +1,19 @@ |
||||
Explicit binding is a way to explicitly state what the `this` keyword is going to be bound to using `call`, `apply` or `bind` methods of a function. |
||||
|
||||
```js |
||||
const roadmap = { |
||||
name: 'JavaScript', |
||||
}; |
||||
|
||||
function printName() { |
||||
console.log(this.name); |
||||
} |
||||
|
||||
printName.call(roadmap); // JavaScript |
||||
printName.apply(roadmap); // JavaScript |
||||
|
||||
const printRoadmapName = printName.bind(roadmap); |
||||
printRoadmapName(); // JavaScript |
||||
``` |
||||
|
||||
In the above example, the `this` keyword inside the `printName()` function is explicitly bound to the `roadmap` object using `call`, `apply` or `bind` methods. |
@ -0,0 +1,17 @@ |
||||
You can run some codes on interval using `setInterval` function in JavaScript. It accepts a function and a time interval in milliseconds. It returns a unique id which you can use to clear the interval using `clearInterval` function. |
||||
|
||||
```js |
||||
const intervalId = setInterval(() => { |
||||
console.log('Hello World'); |
||||
}, 1000); |
||||
|
||||
// Output: |
||||
// Hello World |
||||
// Hello World |
||||
``` |
||||
|
||||
In the above code, the `setInterval` function runs the callback function every 1000 milliseconds (1 second) and prints `Hello World` to the console. It returns a unique id which you can use to clear the interval using `clearInterval` function. |
||||
|
||||
```js |
||||
clearInterval(intervalId); |
||||
``` |
@ -0,0 +1,16 @@ |
||||
To run a piece of code after a certain time, you can use `setTimeout` function in JavaScript. It accepts a function and a time interval in milliseconds. It returns a unique id which you can use to clear the timeout using `clearTimeout` function. |
||||
|
||||
```js |
||||
const timeoutId = setTimeout(() => { |
||||
console.log('Hello World'); |
||||
}, 1000); |
||||
|
||||
// Output: |
||||
// Hello World |
||||
``` |
||||
|
||||
In the above code, the `setTimeout` function runs the callback function after 1000 milliseconds (1 second) and prints `Hello World` to the console. It returns a unique id which you can use to clear the timeout using `clearTimeout` function. |
||||
|
||||
```js |
||||
clearTimeout(timeoutId); |
||||
``` |
Loading…
Reference in new issue