Skip to content

Commit 8e0d379

Browse files
committed
Version 1.4.0
1 parent 11cf6c1 commit 8e0d379

File tree

1,961 files changed

+38664
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,961 files changed

+38664
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## [Can I avoid using postMessages completely](#can-i-avoid-using-postmessages-completely)
2+
3+
### Can I avoid using postMessages completely?
4+
5+
While `postMessage` is a useful API for cross-origin communication, alternatives like `localStorage`, `cookies`, or server-side messaging (e.g., WebSockets) can be used depending on the use case. Avoiding `postMessage` may increase security in certain scenarios.
6+
7+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Security](./theme/security)
8+
9+

Diff for: level/advanced/does_javascript_uses_mixins.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## [Does javascript uses mixins](#does-javascript-uses-mixins)
2+
3+
### Does JavaScript use mixins?
4+
5+
Yes, JavaScript can implement mixins, which are objects that provide methods to other objects. A mixin is a way to reuse functionality across different objects without using inheritance.
6+
7+
Example:
8+
9+
```javascript
10+
let mixin = {
11+
greet() { console.log('Hello!'); }
12+
};
13+
14+
let obj = Object.assign({}, mixin);
15+
obj.greet(); // Output: 'Hello!'
16+
```
17+
18+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Object-Oriented Programming](./theme/object_oriented_programming)
19+
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## [Find Duplicates in an Array Without Extra Space](#find-duplicates-in-an-array-without-extra-space)
2+
3+
### Find Duplicates in an Array Without Extra Space
4+
5+
To find duplicates without using extra space, you can modify the array in-place.
6+
7+
#### Algorithm:
8+
1. Iterate through the array and use the value of each element as an index.
9+
2. If the value at that index is negative, it means the element has been seen before, so it is a duplicate.
10+
3. If the value at that index is positive, make it negative to mark it as visited.
11+
4. Return the duplicates.
12+
13+
#### Example:
14+
```javascript
15+
function findDuplicatesInPlace(arr) {
16+
const duplicates = [];
17+
for (let i = 0; i < arr.length; i++) {
18+
const absVal = Math.abs(arr[i]);
19+
if (arr[absVal] < 0) {
20+
duplicates.push(absVal);
21+
} else {
22+
arr[absVal] = -arr[absVal];
23+
}
24+
}
25+
return duplicates;
26+
}
27+
28+
// Example usage
29+
console.log(findDuplicatesInPlace([4, 3, 2, 7, 8, 2, 3, 1])); // Output: [2, 3]
30+
console.log(findDuplicatesInPlace([1, 1, 1, 2, 2])); // Output: [1, 2]
31+
```
32+
33+
This method has a time complexity of O(n), where n is the length of the array, and does not use extra space.
34+
35+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Arrays](./theme/arrays), [Algorithm](./theme/algorithm)
36+
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## [Give an example of statements affected by automatic semicolon insertion?](#give-an-example-of-statements-affected-by-automatic-semicolon-insertion)
2+
3+
### Give an example of statements affected by automatic semicolon insertion
4+
5+
Automatic Semicolon Insertion (ASI) can lead to unexpected behavior when a semicolon is inserted incorrectly.
6+
7+
Example:
8+
```javascript
9+
function test() {
10+
return
11+
{
12+
key: 'value'
13+
};
14+
}
15+
16+
console.log(test()); // undefined
17+
```
18+
19+
Explanation:
20+
- ASI inserts a semicolon after `return`, so the object is never returned. The correct version:
21+
22+
```javascript
23+
function test() {
24+
return {
25+
key: 'value'
26+
};
27+
}
28+
29+
console.log(test()); // { key: 'value' }
30+
```
31+
32+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Syntax](./theme/syntax)
33+
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## [How do you avoid receiving postMessages from attackers](#how-do-you-avoid-receiving-postmessages-from-attackers)
2+
3+
### How do you avoid receiving postMessages from attackers?
4+
5+
To prevent receiving messages from attackers, always validate the `origin` property of the message and ensure it matches the trusted source. You can also check the message's content for additional security.
6+
7+
Example:
8+
9+
```javascript
10+
window.addEventListener('message', (event) => {
11+
if (event.origin !== 'https://trusted.com') {
12+
return;
13+
}
14+
// Process message
15+
});
16+
```
17+
18+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Security](./theme/security)
19+
20+

Diff for: level/advanced/how_do_you_compare_object_and_map.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## [How do you compare Object and Map](#how-do-you-compare-object-and-map)
2+
3+
### How do you compare Object and Map?
4+
5+
Both `Object` and `Map` are key-value stores in JavaScript, but they have differences:
6+
7+
1. **Key Types**: `Map` can use any data type as a key, while `Object` keys are always converted to strings.
8+
2. **Iteration**: `Map` preserves the order of insertion, while `Object` does not guarantee order.
9+
3. **Performance**: `Map` is generally more efficient for frequent additions and removals of key-value pairs.
10+
11+
**Tags**: [advanced](./level/advanced), [Object](./theme/object), [Map](./theme/map), [JavaScript](./theme/javascript)
12+
13+
**URL**: [https://www.tiktok.com/@jsmentoring/photo/7447122335863147808](https://www.tiktok.com/@jsmentoring/photo/7447122335863147808)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## [How do you create custom HTML element?](#how-do-you-create-custom-html-element)
2+
3+
### How do you create custom HTML element?
4+
5+
You can create a custom HTML element using the `customElements.define` method. Here's an example:
6+
7+
```javascript
8+
class MyElement extends HTMLElement {
9+
constructor() {
10+
super();
11+
this.innerHTML = '<p>Hello, Custom Element!</p>';
12+
}
13+
}
14+
15+
customElements.define('my-element', MyElement);
16+
17+
// Usage in HTML:
18+
// <my-element></my-element>
19+
```
20+
21+
This defines a new custom element `<my-element>` which can be used like a standard HTML tag.
22+
23+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Web Components](./theme/web_components)
24+
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## [How do you create polyfills for map, filter and reduce methods?](#how-do-you-create-polyfills-for-map-filter-and-reduce-methods)
2+
3+
### How do you create polyfills for `map`, `filter`, and `reduce` methods?
4+
5+
#### `map` Polyfill:
6+
```javascript
7+
Array.prototype.myMap = function(callback) {
8+
const result = [];
9+
for (let i = 0; i < this.length; i++) {
10+
if (this.hasOwnProperty(i)) {
11+
result.push(callback(this[i], i, this));
12+
}
13+
}
14+
return result;
15+
};
16+
```
17+
18+
#### `filter` Polyfill:
19+
```javascript
20+
Array.prototype.myFilter = function(callback) {
21+
const result = [];
22+
for (let i = 0; i < this.length; i++) {
23+
if (this.hasOwnProperty(i) && callback(this[i], i, this)) {
24+
result.push(this[i]);
25+
}
26+
}
27+
return result;
28+
};
29+
```
30+
31+
#### `reduce` Polyfill:
32+
```javascript
33+
Array.prototype.myReduce = function(callback, initialValue) {
34+
let accumulator = initialValue;
35+
for (let i = 0; i < this.length; i++) {
36+
if (this.hasOwnProperty(i)) {
37+
accumulator = callback(accumulator, this[i], i, this);
38+
}
39+
}
40+
return accumulator;
41+
};
42+
```
43+
44+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Polyfills](./theme/polyfills)
45+
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## [How do you create your own bind method using either call or apply method?](#how-do-you-create-your-own-bind-method-using-either-call-or-apply-method)
2+
3+
### How do you create your own bind method using either call or apply method?
4+
5+
You can create a custom `bind` method by using `call` or `apply` to explicitly set the `this` context.
6+
7+
Example:
8+
9+
```javascript
10+
Function.prototype.customBind = function(context, ...args) {
11+
const fn = this;
12+
return function(...innerArgs) {
13+
return fn.apply(context, [...args, ...innerArgs]);
14+
};
15+
};
16+
17+
function greet(greeting, name) {
18+
console.log(`${greeting}, ${name}!`);
19+
}
20+
21+
const boundGreet = greet.customBind(null, 'Hello');
22+
boundGreet('Alice'); // 'Hello, Alice!'
23+
```
24+
25+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Functions](./theme/functions)
26+
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## [How do you make an object iterable in JavaScript](#how-do-you-make-an-object-iterable-in-javascript)
2+
3+
### How do you make an object iterable in JavaScript?
4+
5+
To make an object iterable, you need to implement the `[Symbol.iterator]` method, which returns an iterator object.
6+
7+
Example:
8+
9+
```javascript
10+
const iterableObject = {
11+
data: [1, 2, 3],
12+
[Symbol.iterator]() {
13+
let index = 0;
14+
let data = this.data;
15+
return {
16+
next() {
17+
return index < data.length
18+
? { value: data[index++], done: false }
19+
: { done: true };
20+
}
21+
};
22+
}
23+
};
24+
25+
for (let value of iterableObject) {
26+
console.log(value); // Output: 1, 2, 3
27+
}
28+
```
29+
30+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Iterables](./theme/iterables)
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## [How do you prevent promises swallowing errors](#how-do-you-prevent-promises-swallowing-errors)
2+
3+
### How do you prevent promises from swallowing errors?
4+
5+
To prevent promises from swallowing errors, ensure that you handle rejections using `.catch()` or use `try-catch` with `async-await` syntax. Additionally, always return or throw errors explicitly.
6+
7+
Example:
8+
9+
```javascript
10+
Promise.resolve()
11+
.then(() => { throw new Error('Error occurred'); })
12+
.catch(error => console.error('Caught error:', error));
13+
14+
async function asyncFunction() {
15+
try {
16+
await Promise.reject('Error occurred');
17+
} catch (error) {
18+
console.error('Caught error:', error);
19+
}
20+
}
21+
asyncFunction();
22+
```
23+
24+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Promises](./theme/promises)
25+
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## [How do you reuse information across service worker restarts](#how-do-you-reuse-information-across-service-worker-restarts)
2+
3+
### How do you reuse information across service worker restarts?
4+
5+
To reuse information across service worker restarts, store data in persistent storage mechanisms like IndexedDB or localStorage. This ensures data remains accessible even when the service worker restarts.
6+
7+
**Tags**: [advanced](./level/advanced), [service worker](./theme/service_worker), [persistence](./theme/persistence), [IndexedDB](./theme/indexeddb)
8+
9+
**URL**: [https://www.tiktok.com/@jsmentoring/photo/7447841580880350497](https://www.tiktok.com/@jsmentoring/photo/7447841580880350497)

Diff for: level/advanced/how_to_cancel_a_fetch_request.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## [How to cancel a fetch request](#how-to-cancel-a-fetch-request)
2+
3+
### How to cancel a fetch request?
4+
5+
You can cancel a fetch request using an `AbortController`. The `AbortController` allows you to abort one or more fetch requests.
6+
7+
Example:
8+
9+
```javascript
10+
let controller = new AbortController();
11+
let signal = controller.signal;
12+
13+
fetch('https://api.example.com/data', { signal: signal })
14+
.then(response => response.json())
15+
.then(data => console.log(data))
16+
.catch(err => console.log('Fetch aborted', err));
17+
18+
// To cancel the request
19+
controller.abort();
20+
```
21+
22+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Fetch API](./theme/fetch_api)
23+
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## [How to detect if a function is called as a constructor](#how-to-detect-if-a-function-is-called-as-constructor)
2+
3+
### How to detect if a function is called as a constructor?
4+
5+
To detect if a function is called as a constructor, check the value of `this`. In a constructor call, `this` refers to the newly created object.
6+
7+
Example:
8+
9+
```javascript
10+
function MyFunction() {
11+
if (!(this instanceof MyFunction)) {
12+
throw new Error('Must be called with new');
13+
}
14+
console.log('Called as constructor');
15+
}
16+
17+
new MyFunction(); // Works
18+
MyFunction(); // Throws error
19+
```
20+
21+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Functions](./theme/functions)
22+
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## [How to invoke an IIFE without any extra brackets?](#how-to-invoke-an-iife-without-any-extra-brackets)
2+
3+
### How to invoke an IIFE without any extra brackets?
4+
5+
Use an **Unary Operator** like `!` or `+` before the function to execute it immediately:
6+
7+
Example:
8+
9+
```javascript
10+
!function() {
11+
console.log('IIFE invoked!');
12+
}();
13+
14+
+function() {
15+
console.log('Another IIFE invoked!');
16+
}();
17+
```
18+
19+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Functions](./theme/functions)
20+
21+

0 commit comments

Comments
 (0)