Dev Dastan
324 subscribers
49 photos
2 videos
41 links
πŸ”Έ Programming Tips and Concepts πŸ‘Ύ

πŸ”ΉContact: @Hossein13M 🀘🏻
Download Telegram
✨ 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.
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.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘8❀7🀯1
πŸ“± Abstract Syntax Trees (AST) in JavaScript


ℹ️ AST in JavaScript represents the syntactic structure of code. It's a hierarchical tree-like structure composed of nodes, each representing a syntactic element like variable declarations or function calls. An AST is created through two key stages:


1️⃣ Lexical Analysis (Tokenization): Code is tokenized by a Lexical Analyzer, breaking it down into tokens. Lexical Analysis: Code is tokenized by a Lexical Analyzer, breaking it down into tokens.

2️⃣ Syntax Analysis (Parsing): Tokens are structured into an AST by a Syntax Analyzer or Parser, enabling deeper code analysis.

βœ”οΈ AST use cases:

πŸ”£Code Analysis: ESLint, JSHint, TypeScript Compiler (TSC), SonarQube

πŸ”£Code Transformation: Babel, TypeScript Compiler (TSC), SWC

πŸ”£Refactoring: TSLint, TypeScript Compiler (TSC)

πŸ”£Code Generation: TypeScript Compiler (TSC), Babel

πŸ”£Language Tooling: Visual Studio Code, WebStorm, Atom, Sublime Text

πŸ”£Optimization: V8 JavaScript Engine, LLVM (Low-Level Virtual Machine)


βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
πŸ’» AST Explorer Tool: [here]
βž–βž–βž–βž–βž–βž–
#JavaScript #computerScience


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘13❀5
πŸ“± JavaScript Signals Standard Proposal (Stage 1)


πŸ•― Signals are a reactive state management mechanism for JavaScript apps. Each Signal is a source of a value. When the value changes, the Signal makes sure all dependent states (or other Signals) are notified and updated.

❗️ Frameworks like Angular, VueJS, SolidJS, and ... heavily utilize Signals. Establishing a standard for JavaScript itself would be beneficial (it is currently at stage 1).

βœ”οΈ Advantages of Signals:

πŸ”£ Simplified Reactive Programming

πŸ”£ Automated State Tracking and Updating

πŸ”£ Efficient Performance

πŸ”£ Cross-Framework Unity


πŸ”  A simple proposed signal example:

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


⚠️ If you have experience using ref in VueJS, understanding Signals should pose no challenge.

⚠️ The purpose of using Signals is to update only the dependencies in the DOM and execute subscriber functions in JavaScript.

βž–βž–βž–βž–βž–βž–
πŸ“± GitHub Proposal Repo: [here]
πŸ“š Article: [here]
πŸ“± YouTube Video: [here]
βž–βž–βž–βž–βž–βž–
#JavaScript #EcmaScript


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘8❀4🀯2
πŸ“± JavaScript Quiz

⁉️ What is the output of this code?

function Car() {
this.make = 'Lamborghini';
return { make: 'Maserati' };
}

const myCar = new Car();
console.log(myCar.make);


βœ”οΈ Answer:
When a constructor function is invoked with the new keyword, it creates an object and sets this to refer to that object. If the constructor function doesn't explicitly return anything, it defaults to returning the newly created object. In the case of the constructor function Car, it explicitly returns a new object with 'make' set to "Maserati", overriding the default behavior. Therefore, when new Car() is called, the returned object is assigned to myCar, resulting in the output "Maserati" when myCar.make is accessed.

βž–βž–βž–βž–βž–βž–
#JavaScript #Quiz


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
🀯10πŸ‘9❀4
πŸ“± Promise.all vs Promise.allSettled


πŸ•― Promise.all and Promise.allSettled are two methods provided by JavaScript's Promise API, but they have different behaviors and use cases.

πŸ”£ Promise.all
Waits for all promises to resolve or rejects immediately if any promise rejects. Resolves with an array of resolved values.


πŸ”£ Promise.allSettled
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.


βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
πŸ“± YouTube Video: [here]
πŸ“± Image Credit (on X.com): [here]
βž–βž–βž–βž–βž–βž–
#JavaScript


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
❀8πŸ‘3
πŸ“± JavaScript Quiz

⁉️ What's the value of num?

const num = parseInt('7*6', 10);


πŸ”€ 42
πŸ”€ "42"
πŸ”€ 7
πŸ”€ NaN

βœ”οΈ Answer:
parseInt returns only the first number in the string. Depending on the radix (the second argument specifying the number type: base 10, hexadecimal, octal, binary, etc.), parseInt validates the characters in the string. If it encounters an invalid character, it stops parsing and ignores the rest.

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.