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
@@ -1290,7 +1290,7 @@ These days `fetch()` is preferred for its cleaner syntax and modern features.
1290
1290
1291
1291
`AbortController` is used to cancel ongoing asynchronous operations like fetch requests.
1292
1292
1293
-
```js
1293
+
```js live
1294
1294
constcontroller=newAbortController();
1295
1295
constsignal=controller.signal;
1296
1296
@@ -1641,7 +1641,7 @@ In JavaScript, iterators and generators are powerful tools for managing sequence
1641
1641
1642
1642
Here's an example of an object implementing the iterator interface.
1643
1643
1644
-
```js
1644
+
```js live
1645
1645
constiterator= {
1646
1646
current:0,
1647
1647
last:5,
@@ -1663,7 +1663,7 @@ while (!result.done) {
1663
1663
1664
1664
**Generators** are a special functions that **can pause execution and resume at a later point**. It uses the `function*` syntax and the `yield` keyword to control the flow of execution. When you call a generator function, it doesn't execute completely like a regular function. Instead, it returns an iterator object. Calling the `next()` method on the returned iterator advances the generator to the next `yield` statement, and the value after `yield` becomes the return value of `next()`.
1665
1665
1666
-
```js
1666
+
```js live
1667
1667
function*numberGenerator() {
1668
1668
let num =0;
1669
1669
while (num <=5) {
@@ -1703,7 +1703,7 @@ Generators are powerful for creating iterators on-demand, especially for infinit
1703
1703
1704
1704
**Mutable objects** allow for modification of properties and values after creation, which is the default behavior for most objects.
**Immutable objects** cannot be directly modified after creation. Its content cannot be changed without creating an entirely new value.
1720
1720
1721
-
```js
1721
+
```js live
1722
1722
constimmutableObject=Object.freeze({
1723
1723
name:'John',
1724
1724
age:30,
@@ -2003,7 +2003,7 @@ Getters and setters are defined using the `get` and `set` keywords, respectively
2003
2003
2004
2004
Here's a code example demonstrating the use of getters and setters:
2005
2005
2006
-
```js
2006
+
```js live
2007
2007
constperson= {
2008
2008
_name:'John Doe', // Private property
2009
2009
@@ -2050,7 +2050,7 @@ In JavaScript, a proxy is an object that acts as an intermediary between an obje
2050
2050
2051
2051
Here's a basic example of using a `Proxy` to log every property access:
2052
2052
2053
-
```js
2053
+
```js live
2054
2054
constmyObject= {
2055
2055
name:'John',
2056
2056
age:42,
@@ -2065,11 +2065,13 @@ const handler = {
2065
2065
2066
2066
constproxiedObject=newProxy(myObject, handler);
2067
2067
2068
-
console.log(proxiedObject.name);// 'John'
2068
+
console.log(proxiedObject.name);
2069
2069
// Someone accessed property "name"
2070
+
// 'John'
2070
2071
2071
-
console.log(proxiedObject.age);// 42
2072
+
console.log(proxiedObject.age);
2072
2073
// Someone accessed property "age"
2074
+
// 42
2073
2075
```
2074
2076
2075
2077
Use cases include:
@@ -2442,7 +2444,7 @@ In JavaScript, a proxy is an object that acts as an intermediary between an obje
2442
2444
2443
2445
Here's a basic example of using a `Proxy` to log every property access:
2444
2446
2445
-
```js
2447
+
```js live
2446
2448
constmyObject= {
2447
2449
name:'John',
2448
2450
age:42,
@@ -2457,11 +2459,13 @@ const handler = {
2457
2459
2458
2460
constproxiedObject=newProxy(myObject, handler);
2459
2461
2460
-
console.log(proxiedObject.name);// 'John'
2462
+
console.log(proxiedObject.name);
2461
2463
// Someone accessed property "name"
2464
+
// 'John'
2462
2465
2463
-
console.log(proxiedObject.age);// 42
2466
+
console.log(proxiedObject.age);
2464
2467
// Someone accessed property "age"
2468
+
// 42
2465
2469
```
2466
2470
2467
2471
Use cases include:
@@ -2536,7 +2540,7 @@ The following behavior summarizes the result of accessing the variables before t
2536
2540
2537
2541
Hoisting in JavaScript means that function declarations are moved to the top of their containing scope during the compile phase, making them available throughout the entire scope. This allows you to call a function before it is defined in the code. However, function expressions are not hoisted in the same way. If you try to call a function expression before it is defined, you will get an error because the variable holding the function is hoisted but not its assignment.
2538
2542
2539
-
```js
2543
+
```js live
2540
2544
// Function declaration
2541
2545
console.log(foo()); // Works fine
2542
2546
functionfoo() {
@@ -2912,7 +2916,7 @@ In JavaScript, iterators and generators are powerful tools for managing sequence
2912
2916
2913
2917
Here's an example of an object implementing the iterator interface.
2914
2918
2915
-
```js
2919
+
```js live
2916
2920
constiterator= {
2917
2921
current:0,
2918
2922
last:5,
@@ -2934,7 +2938,7 @@ while (!result.done) {
2934
2938
2935
2939
**Generators** are a special functions that **can pause execution and resume at a later point**. It uses the `function*` syntax and the `yield` keyword to control the flow of execution. When you call a generator function, it doesn't execute completely like a regular function. Instead, it returns an iterator object. Calling the `next()` method on the returned iterator advances the generator to the next `yield` statement, and the value after `yield` becomes the return value of `next()`.
2936
2940
2937
-
```js
2941
+
```js live
2938
2942
function*numberGenerator() {
2939
2943
let num =0;
2940
2944
while (num <=5) {
@@ -3512,7 +3516,7 @@ if (obj.hasOwnProperty('key')) {
3512
3516
3513
3517
**Mutable objects** allow for modification of properties and values after creation, which is the default behavior for most objects.
`Object.seal()` is used to prevent new properties from being added to an object and to mark all existing properties as non-configurable. This means you can still modify the values of existing properties, but you cannot delete them or add new ones.
3605
+
`Object.seal()` is used to prevent new properties from being added to an object and to mark all existing properties as non-configurable. This means you can still modify the values of existing properties, but you cannot delete them or add new ones. Doing so will throw errors in strict mode but fail silently in non-strict mode. In the following examples, you can uncomment the 'use strict' comment to see this.
3606
+
3607
+
```js live
3608
+
// 'use strict'
3602
3609
3603
-
```js
3604
3610
constobj= { name:'John' };
3605
3611
Object.seal(obj);
3606
3612
3607
3613
obj.name='Jane'; // Allowed
3608
-
obj.age=30; // Not allowed
3609
-
deleteobj.name; // Not allowed
3614
+
obj.age=30; // Not allowed, throws an error in strict mode
3615
+
deleteobj.name; // Not allowed, throws an error in strict mode
@@ -3623,7 +3631,7 @@ delete obj.name; // Not allowed
3623
3631
3624
3632
`Object.preventExtensions()` is a method in JavaScript that prevents new properties from being added to an object. However, it does not affect the deletion or modification of existing properties. This method is useful when you want to ensure that an object remains in a certain shape and no additional properties can be added to it.
3625
3633
3626
-
```js
3634
+
```js live
3627
3635
constobj= { name:'John' };
3628
3636
Object.preventExtensions(obj);
3629
3637
@@ -3649,7 +3657,7 @@ Getters and setters are defined using the `get` and `set` keywords, respectively
3649
3657
3650
3658
Here's a code example demonstrating the use of getters and setters:
`async/await` is a modern syntax in JavaScript that simplifies working with promises. By using the `async` keyword before a function, you can use the `await` keyword inside that function to pause execution until a promise is resolved. This makes asynchronous code look and behave more like synchronous code, making it easier to read and maintain.
@@ -5208,7 +5219,7 @@ These days `fetch()` is preferred for its cleaner syntax and modern features.
5208
5219
5209
5220
`AbortController` is used to cancel ongoing asynchronous operations like fetch requests.
5210
5221
5211
-
```js
5222
+
```js live
5212
5223
constcontroller=newAbortController();
5213
5224
constsignal=controller.signal;
5214
5225
@@ -5296,13 +5307,15 @@ There are three main types of workers in JavaScript:
5296
5307
5297
5308
The WebSocket API provides a way to open a persistent connection between a client and a server, allowing for real-time, two-way communication. Unlike HTTP, which is request-response based, WebSocket enables full-duplex communication, meaning both the client and server can send and receive messages independently. This is particularly useful for applications like chat apps, live updates, and online gaming.
socket.addEventListener('open', function (event) {
5305
-
socket.send('Hello Server!');
5318
+
socket.send('Hello Server!');// Sends the message to the Postman WebSocket server
5306
5319
});
5307
5320
5308
5321
// Event listener for when a message is received from the server
@@ -5385,7 +5398,7 @@ To detect if JavaScript is disabled on a page, you can use the `<noscript>` HTML
5385
5398
5386
5399
The `Intl` namespace object in JavaScript is used for internationalization purposes. It provides language-sensitive string comparison, number formatting, and date and time formatting. For example, you can use `Intl.DateTimeFormat` to format dates according to a specific locale:
5387
5400
5388
-
```js
5401
+
```js live
5389
5402
constdate=newDate();
5390
5403
constformatter=newIntl.DateTimeFormat('en-US');
5391
5404
console.log(formatter.format(date)); // Outputs date in 'MM/DD/YYYY' format
@@ -6546,7 +6559,7 @@ The Factory pattern is a design pattern used to create objects without specifyin
6546
6559
6547
6560
For example, in JavaScript, you can use a factory function to create different types of objects:
6548
6561
6549
-
```js
6562
+
```js live
6550
6563
functioncreateAnimal(type) {
6551
6564
if (type ==='dog') {
6552
6565
return { sound:'woof' };
@@ -6587,7 +6600,7 @@ The Observer pattern is a design pattern where an object, known as the subject,
6587
6600
6588
6601
The Module pattern in JavaScript is a design pattern used to create self-contained modules of code. It helps with encapsulation by allowing you to define private and public members within a module. Private members are not accessible from outside the module, while public members are exposed through a returned object. This pattern helps in organizing code, avoiding global namespace pollution, and maintaining a clean separation of concerns.
6589
6602
6590
-
```js
6603
+
```js live
6591
6604
var myModule = (function () {
6592
6605
var privateVar ='I am private';
6593
6606
@@ -6646,7 +6659,7 @@ The Decorator pattern is a structural design pattern that allows behavior to be
6646
6659
6647
6660
For example, if you have a `Car` class and you want to add features like `GPS` or `Sunroof` without modifying the `Car` class itself, you can create decorators for these features.
@@ -100,8 +100,8 @@ In modern browsers, AJAX is done using the `fetch()` API instead of `XMLHTTPRequ
100
100
2.**Return a promise**: The `fetch()` function returns a `Promise` that resolves to a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object representing the response from the server. This `Promise needs` to be handled using `.then()` or `async/await`.
101
101
3.**Handling the response**: The `Response` object provides methods to define how the body content should be handled, such as `.json()` for parsing JSON data, `.text()` for plain text, `.blob()` for binary data, etc.
0 commit comments