β¨ Object.freeze vs Object.seal in JavaScript
β Object.seal prevents adding and/or removing properties from the sealed object and makes every existing property non-configurable.
β Object.freeze does Exactly what Object.seal do, plus It prevents modifying any existing properties.
βοΈ There are Object.isFrozen() and Object.isSealed() methods to check if an object is frozen or sealed.
ββββββ
πΈ Image Credit: [here]
π Stack Overflow Answer: [here]
ββββββ
#JavaScript #Programming
π₯ Follow @devDastan for more content.
ββββββ
ββββββ
#JavaScript #Programming
Please open Telegram to view this post
VIEW IN TELEGRAM
π15β€9π3π1
β¨ JavaScript's Garbage Collection
βΉοΈ The garbage collector in JavaScript is responsible for reclaiming memory occupied by objects that are no longer needed. This mechanism is done using mark-and-sweep algorithm.
βΉοΈ The mark-and-sweep algorithm consists of two main phases:
1οΈβ£ Marking: Garbage collector marks reachable objects from the global object, ensuring none are left unmarked if reachable from a rooted object.
2οΈβ£ Sweeping: Garbage collector reclaims memory from unmarked objects, freeing it up for future use.
π Learn more about the JS's garbage collector's mechanism and best practices on the Medium I have written.
ββββββ
π Article (Written by me!): [here]
ββββββ
#JavaScript #Algorithm
π₯ Follow @devDastan for more content.
ββββββ
ββββββ
#JavaScript #Algorithm
Please open Telegram to view this post
VIEW IN TELEGRAM
π8β€7π€―1
ββββββ
ββββββ
#JavaScript #computerScience
Please open Telegram to view this post
VIEW IN TELEGRAM
π13β€5
const counter = new Signal.State(0);
// Read the value of Signal
console.log(counter.get()); // Output: 0
// Change the value of Signal
counter.set(1);
console.log(counter.get()); // Output: 1
ref in VueJS, understanding Signals should pose no challenge.ββββββ
ββββββ
#JavaScript #EcmaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
π8β€4π€―2
function Car() {
this.make = 'Lamborghini';
return { make: 'Maserati' };
}
const myCar = new Car();
console.log(myCar.make);ββββββ
#JavaScript #Quiz
Please open Telegram to view this post
VIEW IN TELEGRAM
π€―10π9β€4
Promise.all and Promise.allSettled are two methods provided by JavaScript's Promise API, but they have different behaviors and use cases.Waits for all promises to resolve or rejects immediately if any promise rejects. Resolves with an array of resolved values.
Waits for all promises to settle (resolve or reject) and always resolves. Returns an array of objects representing the outcomes of the promises, including their status and value/reason.
ββββββ
ββββββ
#JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
β€8π3
num?const num = parseInt('7*6', 10);For example, in "78", only "7" is valid, so it parses it into the decimal 7, ignoring the "" and "8". Thus, num now equals 7.
ββββββ
#JavaScript #Quiz
Please open Telegram to view this post
VIEW IN TELEGRAM
π8π€―5
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}import { useRef } from 'react';
function InputFocus() {
const inputRef = useRef(null);
function handleFocus() {
inputRef.current.focus();
}
return (
<div>
<input ref={inputRef} />
<button onClick={handleFocus}>Focus Input</button>
</div>
);
}- State is immutable
- Avoid reading/writing refs during rendering
- Both persist data and have different update behaviors
ββββββ
ββββββ
#JavaScript #ReactJS
Please open Telegram to view this post
VIEW IN TELEGRAM
π11π1
const user = {
email: "[email protected]",
updateEmail: email => {
this.email = email;
}
}
user.updateEmail("[email protected]")
console.log(user.email)[email protected][email protected]ββββββ
#JavaScript #Quiz
Please open Telegram to view this post
VIEW IN TELEGRAM
π9π€―5β€4
const box = { x: 10, y: 20 };
Object.freeze(box);
const shape = box;
shape.x = 100;
console.log(shape);
ReferenceErrorObject.freeze prevents adding, removing, or modifying properties of an object (unless the propertyβs value is another object).
When we create the variable shape and set it equal to the frozen object box, shape also refers to a frozen object. You can check if an object is frozen using Object.isFrozen. In this case, Object.isFrozen(shape) returns true.
Since shape is frozen and x is not an object, we cannot modify x. Therefore, x remains 10, and { x: 10, y: 20 } gets logged.
ββββββ
#JavaScript #Quiz
Please open Telegram to view this post
VIEW IN TELEGRAM
π16π2π€―1
<div onclick="console.log('first div')">
<div onclick="console.log('second div')">
<button onclick="console.log('button')">
Click!
</button>
</div>
</div>
The deepest nested element that caused the event is the target of the event. You can stop bubbling by event.stopPropagation
ββββββ
#JavaScript #Quiz
Please open Telegram to view this post
VIEW IN TELEGRAM
π€―5π4β€3
ββββββ
ββββββ
#JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
π5