Skip to content

Commit 16265c7

Browse files
Executable coding blocks production-readiness - PR 3 (#23)
Co-authored-by: GitHub Actions <github-actions[bot]@users.noreply.github.com>
1 parent 3fcfe79 commit 16265c7

File tree

22 files changed

+196
-123
lines changed
  • questions
    • explain-the-concept-of-the-prototype-pattern
    • explain-the-concept-of-the-singleton-pattern
    • how-can-you-create-custom-error-objects
    • how-do-sets-and-maps-handle-equality-checks-for-objects
    • how-do-you-get-the-query-string-values-of-the-current-page-in-javascript
    • how-do-you-handle-errors-using-trycatch-blocks
    • what-are-design-patterns-and-why-are-they-useful
    • what-are-javascript-object-getters-and-setters-for
    • what-are-sets-and-maps-and-how-are-they-used
    • what-are-symbols-used-for
    • what-are-the-common-pitfalls-of-using-the-this-keyword
    • what-are-the-differences-between-map-set-and-weakmap-weakset
    • what-are-the-different-types-of-errors-in-javascript
    • what-are-the-potential-issues-caused-by-hoisting
    • what-are-the-potential-pitfalls-of-using-closures
    • what-is-a-closure-and-how-why-would-you-use-one
    • what-is-currying-and-how-does-it-work
    • what-is-the-difference-between-a-map-object-and-a-plain-object-in-javascript
    • what-is-the-prototype-chain-and-how-does-it-work
    • why-you-might-want-to-create-static-class-members

22 files changed

+196
-123
lines changed

README.md

+30-23
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre
853853
854854
In simple terms, functions have access to variables that were in their scope at the time of their creation. This is what we call the function's lexical scope. A closure is a function that retains access to these variables even after the outer function has finished executing. This is like the function has a memory of its original environment.
855855

856-
```js
856+
```js live
857857
function outerFunction() {
858858
const outerVar = 'I am outside of innerFunction';
859859

@@ -1822,7 +1822,7 @@ On the other hand, `WeakSet` only allows objects as elements, and these object e
18221822

18231823
Static class members (properties/methods) has a `static` keyword prepended. Such members cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
18241824

1825-
```js
1825+
```js live
18261826
class Car {
18271827
static noOfWheels = 4;
18281828
static compare() {
@@ -1855,7 +1855,7 @@ Static members are useful under the following scenarios:
18551855

18561856
`Symbol`s in JavaScript are a new primitive data type introduced in ES6 (ECMAScript 2015). They are unique and immutable identifiers that is primarily for object property keys to avoid name collisions. These values can be created using `Symbol(...)` function, and each `Symbol` value is guaranteed to be unique, even if they have the same key/description. `Symbol` properties are not enumerable in `for...in` loops or `Object.keys()`, making them suitable for creating private/internal object state.
18571857

1858-
```js
1858+
```js live
18591859
let sym1 = Symbol();
18601860
let sym2 = Symbol('myKey');
18611861

@@ -2003,7 +2003,7 @@ Getters and setters are defined using the `get` and `set` keywords, respectively
20032003

20042004
Here's a code example demonstrating the use of getters and setters:
20052005

2006-
```js
2006+
```js live
20072007
const person = {
20082008
_name: 'John Doe', // Private property
20092009

@@ -2410,7 +2410,7 @@ console.log(combinedObj); // { a: 1, b: 2, c: 3, d: 4 }
24102410

24112411
`Symbol`s in JavaScript are a new primitive data type introduced in ES6 (ECMAScript 2015). They are unique and immutable identifiers that is primarily for object property keys to avoid name collisions. These values can be created using `Symbol(...)` function, and each `Symbol` value is guaranteed to be unique, even if they have the same key/description. `Symbol` properties are not enumerable in `for...in` loops or `Object.keys()`, making them suitable for creating private/internal object state.
24122412

2413-
```js
2413+
```js live
24142414
let sym1 = Symbol();
24152415
let sym2 = Symbol('myKey');
24162416

@@ -2564,11 +2564,11 @@ var bar = function () {
25642564

25652565
Hoisting can lead to unexpected behavior in JavaScript because variable and function declarations are moved to the top of their containing scope during the compilation phase. This can result in `undefined` values for variables if they are used before their declaration and can cause confusion with function declarations and expressions. For example:
25662566

2567-
```js
2567+
```js live
25682568
console.log(a); // undefined
25692569
var a = 5;
25702570

2571-
console.log(b); // ReferenceError: b is not defined
2571+
console.log(b); // ReferenceError: Cannot access 'b' before initialization
25722572
let b = 10;
25732573
```
25742574

@@ -3647,7 +3647,7 @@ Getters and setters are defined using the `get` and `set` keywords, respectively
36473647

36483648
Here's a code example demonstrating the use of getters and setters:
36493649

3650-
```js
3650+
```js live
36513651
const person = {
36523652
_name: 'John Doe', // Private property
36533653

@@ -4122,7 +4122,7 @@ Things to note are:
41224122

41234123
The prototype chain is a mechanism in JavaScript that allows objects to inherit properties and methods from other objects. When you try to access a property on an object, JavaScript will first look for the property on the object itself. If it doesn't find it, it will look at the object's prototype, and then the prototype's prototype, and so on, until it either finds the property or reaches the end of the chain, which is `null`.
41244124

4125-
```js
4125+
```js live
41264126
function Person(name) {
41274127
this.name = name;
41284128
}
@@ -4388,7 +4388,7 @@ The main takeaway here is that `this` can be changed for a normal function, but
43884388

43894389
Static class members (properties/methods) has a `static` keyword prepended. Such members cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
43904390

4391-
```js
4391+
```js live
43924392
class Car {
43934393
static noOfWheels = 4;
43944394
static compare() {
@@ -4423,7 +4423,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre
44234423
44244424
In simple terms, functions have access to variables that were in their scope at the time of their creation. This is what we call the function's lexical scope. A closure is a function that retains access to these variables even after the outer function has finished executing. This is like the function has a memory of its original environment.
44254425

4426-
```js
4426+
```js live
44274427
function outerFunction() {
44284428
const outerVar = 'I am outside of innerFunction';
44294429

@@ -5507,12 +5507,13 @@ window.location.replace('https://www.example.com');
55075507

55085508
To get the query string values of the current page in JavaScript, you can use the `URLSearchParams` object. First, create a `URLSearchParams` instance with `window.location.search`, then use the `get` method to retrieve specific query parameters. For example:
55095509

5510-
```js
5510+
```js live
55115511
const params = new URLSearchParams(window.location.search);
5512-
const value = params.get('key');
5512+
const value = params.get('language');
5513+
console.log(value);
55135514
```
55145515

5515-
This will give you the value of the query parameter named `key`.
5516+
This will give you the value of the query parameter named `language`. If you look at the URL of this page, you can see that the `language` parameter is set to 'js'.
55165517

55175518
<!-- Update here: /questions/how-do-you-get-the-query-string-values-of-the-current-page-in-javascript/en-US.mdx -->
55185519

@@ -5849,7 +5850,7 @@ try {
58495850

58505851
To create custom error objects in JavaScript, you can extend the built-in `Error` class. This allows you to add custom properties and methods to your error objects. Here's a quick example:
58515852

5852-
```js
5853+
```js live
58535854
class CustomError extends Error {
58545855
constructor(message) {
58555856
super(message);
@@ -5909,7 +5910,7 @@ try {
59095910

59105911
Currying is a technique in functional programming where a function that takes multiple arguments is transformed into a series of functions that each take a single argument. This allows for partial application of functions. For example, a function `f(a, b, c)` can be curried into `f(a)(b)(c)`. Here's a simple example in JavaScript:
59115912

5912-
```js
5913+
```js live
59135914
function add(a) {
59145915
return function (b) {
59155916
return function (c) {
@@ -5919,8 +5920,13 @@ function add(a) {
59195920
}
59205921

59215922
const addOne = add(1);
5923+
console.log(addOne); // function object
5924+
59225925
const addOneAndTwo = addOne(2);
5923-
const result = addOneAndTwo(3); // result is 6
5926+
console.log(addOneAndTwo); // function object
5927+
5928+
const result = addOneAndTwo(3);
5929+
console.log(result); // Output: 6
59245930
```
59255931

59265932
<!-- Update here: /questions/what-is-currying-and-how-does-it-work/en-US.mdx -->
@@ -6014,10 +6020,11 @@ Currying transforms a function with multiple arguments into a sequence of functi
60146020

60156021
`Set`s and `Map`s are built-in JavaScript objects that help manage collections of data. A `Set` is a collection of unique values, while a `Map` is a collection of key-value pairs where keys can be of any type. `Set`s are useful for storing unique items, and `Map`s are useful for associating values with keys.
60166022

6017-
```js
6023+
```js live
60186024
// Set example
6019-
let mySet = new Set([1, 2, 3, 3]);
6020-
mySet.add(4); // Set {1, 2, 3, 4}
6025+
let mySet = new Set([1, 2, 3, 3]); // Set {1, 2, 3} (duplicate values are not added)
6026+
mySet.add(4);
6027+
console.log(mySet); // Set {1, 2, 3, 4}
60216028

60226029
// Map example
60236030
let myMap = new Map();
@@ -6130,7 +6137,7 @@ Both `Map` objects and plain objects in JavaScript can store key-value pairs, bu
61306137

61316138
`Set`s and `Map`s in JavaScript handle equality checks for objects based on reference equality, not deep equality. This means that two objects are considered equal only if they reference the same memory location. For example, if you add two different object literals with the same properties to a `Set`, they will be treated as distinct entries.
61326139

6133-
```js
6140+
```js live
61346141
const set = new Set();
61356142
const obj1 = { a: 1 };
61366143
const obj2 = { a: 1 };
@@ -6508,7 +6515,7 @@ Design patterns are reusable solutions to common problems in software design. Th
65086515

65096516
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is useful when exactly one object is needed to coordinate actions across the system. In JavaScript, this can be implemented using closures or ES6 classes.
65106517

6511-
```js
6518+
```js live
65126519
class Singleton {
65136520
constructor() {
65146521
if (!Singleton.instance) {
@@ -6613,7 +6620,7 @@ myModule.publicMethod(); // Logs: I am private
66136620

66146621
The Prototype pattern is a creational design pattern used to create new objects by copying an existing object, known as the prototype. This pattern is useful when the cost of creating a new object is more expensive than cloning an existing one. In JavaScript, this can be achieved using the `Object.create` method or by using the `prototype` property of a constructor function.
66156622

6616-
```js
6623+
```js live
66176624
const prototypeObject = {
66186625
greet() {
66196626
console.log('Hello, world!');

questions/describe-the-difference-between-a-cookie-sessionstorage-and-localstorage/en-US.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The CookieStore API is relatively new and may not be supported in all browsers (
100100
- **Access**: Data is accessible within all tabs and windows of the same origin.
101101
- **Security**: All JavaScript on the page have access to values within `localStorage`.
102102

103-
```js
103+
```js live
104104
// Set a value in localStorage.
105105
localStorage.setItem('key', 'value');
106106

@@ -123,7 +123,7 @@ localStorage.clear();
123123
- **Access**: Data is only accessible within the current tab or window. Different tabs or windows with the same page will have different `sessionStorage` objects.
124124
- **Security**: All JavaScript on the same page have access to values within `sessionStorage` for that page.
125125

126-
```js
126+
```js live
127127
// Set a value in sessionStorage.
128128
sessionStorage.setItem('key', 'value');
129129

questions/explain-the-concept-of-the-prototype-pattern/en-US.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Explain the concept of the Prototype pattern
66

77
The Prototype pattern is a creational design pattern used to create new objects by copying an existing object, known as the prototype. This pattern is useful when the cost of creating a new object is more expensive than cloning an existing one. In JavaScript, this can be achieved using the `Object.create` method or by using the `prototype` property of a constructor function.
88

9-
```js
9+
```js live
1010
const prototypeObject = {
1111
greet() {
1212
console.log('Hello, world!');
@@ -35,7 +35,7 @@ In JavaScript, the Prototype pattern can be implemented using the `Object.create
3535

3636
The `Object.create` method creates a new object with the specified prototype object and properties.
3737

38-
```js
38+
```js live
3939
const prototypeObject = {
4040
greet() {
4141
console.log('Hello, world!');
@@ -52,7 +52,7 @@ In this example, `newObject` is created with `prototypeObject` as its prototype.
5252

5353
Another way to implement the Prototype pattern in JavaScript is by using constructor functions and the `prototype` property.
5454

55-
```js
55+
```js live
5656
function Person(name) {
5757
this.name = name;
5858
}

questions/explain-the-concept-of-the-singleton-pattern/en-US.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Explain the concept of the Singleton pattern
66

77
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is useful when exactly one object is needed to coordinate actions across the system. In JavaScript, this can be implemented using closures or ES6 classes.
88

9-
```js
9+
```js live
1010
class Singleton {
1111
constructor() {
1212
if (!Singleton.instance) {
@@ -40,7 +40,7 @@ There are several ways to implement the Singleton pattern in JavaScript. Here ar
4040

4141
#### Using closures
4242

43-
```js
43+
```js live
4444
const Singleton = (function () {
4545
let instance;
4646

@@ -67,7 +67,7 @@ console.log(instance1 === instance2); // true
6767

6868
#### Using ES6 classes
6969

70-
```js
70+
```js live
7171
class Singleton {
7272
constructor() {
7373
if (!Singleton.instance) {

questions/how-can-you-create-custom-error-objects/en-US.mdx

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: How can you create custom error objects?
66

77
To create custom error objects in JavaScript, you can extend the built-in `Error` class. This allows you to add custom properties and methods to your error objects. Here's a quick example:
88

9-
```js
9+
```js live
1010
class CustomError extends Error {
1111
constructor(message) {
1212
super(message);
@@ -43,7 +43,7 @@ class CustomError extends Error {
4343

4444
You can add custom properties to your custom error class to provide more context about the error.
4545

46-
```js
46+
```js live
4747
class CustomError extends Error {
4848
constructor(message, errorCode) {
4949
super(message);
@@ -65,7 +65,7 @@ try {
6565

6666
You can also add custom methods to your custom error class to handle specific error-related logic.
6767

68-
```js
68+
```js live
6969
class CustomError extends Error {
7070
constructor(message, errorCode) {
7171
super(message);
@@ -89,7 +89,15 @@ try {
8989

9090
You can use the `instanceof` operator to check if an error is an instance of your custom error class.
9191

92-
```js
92+
```js live
93+
class CustomError extends Error {
94+
constructor(message, errorCode) {
95+
super(message);
96+
this.name = 'CustomError';
97+
this.errorCode = errorCode;
98+
}
99+
}
100+
93101
try {
94102
throw new CustomError('This is a custom error message', 404);
95103
} catch (error) {

questions/how-do-sets-and-maps-handle-equality-checks-for-objects/en-US.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: How do `Set`s and `Map`s handle equality checks for objects?
66

77
`Set`s and `Map`s in JavaScript handle equality checks for objects based on reference equality, not deep equality. This means that two objects are considered equal only if they reference the same memory location. For example, if you add two different object literals with the same properties to a `Set`, they will be treated as distinct entries.
88

9-
```js
9+
```js live
1010
const set = new Set();
1111
const obj1 = { a: 1 };
1212
const obj2 = { a: 1 };
@@ -29,7 +29,7 @@ In JavaScript, `Set`s and `Map`s use reference equality to determine if two obje
2929

3030
When you add objects to a `Set`, the `Set` will only consider them equal if they are the same object reference.
3131

32-
```js
32+
```js live
3333
const set = new Set();
3434
const obj1 = { a: 1 };
3535
const obj2 = { a: 1 };
@@ -46,7 +46,7 @@ In this example, `obj1` and `obj2` have the same properties, but they are differ
4646

4747
Similarly, when you use objects as keys in a `Map`, the `Map` will only consider them equal if they are the same object reference.
4848

49-
```js
49+
```js live
5050
const map = new Map();
5151
const obj1 = { a: 1 };
5252
const obj2 = { a: 1 };

questions/how-do-you-get-the-query-string-values-of-the-current-page-in-javascript/en-US.mdx

+17-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ title: How do you get the query string values of the current page in JavaScript?
66

77
To get the query string values of the current page in JavaScript, you can use the `URLSearchParams` object. First, create a `URLSearchParams` instance with `window.location.search`, then use the `get` method to retrieve specific query parameters. For example:
88

9-
```js
9+
```js live
1010
const params = new URLSearchParams(window.location.search);
11-
const value = params.get('key');
11+
const value = params.get('language');
12+
console.log(value);
1213
```
1314

14-
This will give you the value of the query parameter named `key`.
15+
This will give you the value of the query parameter named `language`. If you look at the URL of this page, you can see that the `language` parameter is set to 'js'.
1516

1617
---
1718

@@ -24,11 +25,14 @@ The `URLSearchParams` interface provides an easy way to work with query strings.
2425
1. **Create a `URLSearchParams` instance**: Use `window.location.search` to get the query string part of the URL.
2526
2. **Retrieve specific query parameters**: Use the `get` method to get the value of a specific query parameter.
2627

27-
```js
28+
```js live
2829
const params = new URLSearchParams(window.location.search);
29-
const value = params.get('key'); // Replace 'key' with the actual query parameter name
30+
const value = params.get('key'); // Replace 'key' with the actual query parameter name (try 'language' or 'tab' for this page)
31+
console.log(value);
3032
```
3133

34+
If the query parameter does not exist, the `get` method returns `null`.
35+
3236
### Example
3337

3438
Consider a URL like `https://example.com?page=2&sort=asc`. To get the values of `page` and `sort`:
@@ -50,22 +54,26 @@ const values = params.getAll('key'); // Returns an array of values
5054

5155
### Checking for the existence of a parameter
5256

53-
You can use the `has` method to check if a query parameter exists:
57+
You can use the `has` method to check if a query parameter exists. Try to check for the query parameters present in the URL of this page.
5458

55-
```js
59+
```js live
5660
const params = new URLSearchParams(window.location.search);
57-
const hasPage = params.has('page'); // true or false
61+
console.log(params.has('page')); // false
62+
console.log(params.has('language')); // true
63+
console.log(params.has('tab')); // true
5864
```
5965

6066
### Iterating over all parameters
6167

6268
You can iterate over all query parameters using the `forEach` method:
6369

64-
```js
70+
```js live
6571
const params = new URLSearchParams(window.location.search);
6672
params.forEach((value, key) => {
6773
console.log(`${key}: ${value}`);
6874
});
75+
// language: js
76+
// tab: quiz
6977
```
7078

7179
## Further reading

0 commit comments

Comments
 (0)