Skip to content

Instantly share code, notes, and snippets.

View MarwanShehata's full-sized avatar
🎯
"Luck is what happens when preparation meets opportunity.", Seneca

Marwan Shehata MarwanShehata

🎯
"Luck is what happens when preparation meets opportunity.", Seneca
View GitHub Profile
@MarwanShehata
MarwanShehata / coercionProtection
Created March 3, 2024 12:08
You can implement custom behaviors for operators and methods using Symbols. For example, you can use the Symbol.toPrimitive well-known symbol to define how an object should be converted to a primitive value
const obj = {
[Symbol.toPrimitive]: function (hint) {
if (hint === "number") {
return 42;
} else if (hint === "string") {
return "Hello";
} else {
return true;
}
},
@MarwanShehata
MarwanShehata / serializationSymbols
Created March 3, 2024 11:44
Metaprogramming (Advanced): Symbols can be used for advanced metaprogramming techniques like property name creation and dynamic property access. However, these use cases are less common and typically require a deep understanding of JavaScript intern
const serializableSymbol = Symbol.for("serializable");
class User {
constructor(name) {
this.name = name;
this[serializableSymbol] = ["name", "age"]; // Mark 'name' and 'age' for serialization
}
getSerializableProperties() {
const properties = [];
const obj = {
[Symbol("my_key")]: 1,
[Symbol("second_key")]: 2,
enum: 3,
nonEnum: 4,
};
Object.defineProperty(obj, "nonEnum", {
enumerable: false,
});
function binarySearch(list, item) {
let min = 0;
let max = list.length - 1;
let guess;
while (min <= max) {
guess = Math.floor((min + max) / 2);
if (list[guess] === item) {
return guess;
} else {
function joinElements(arr, joinString) {
function recurse(i, resultSoFar) {
resultSoFar += arr[i];
console.log(resultSoFar);
if (i === arr.length - 1) {
return resultSoFar;
} else {
return recurse(i + 1, resultSoFar + joinString);
}
}
@MarwanShehata
MarwanShehata / Memoi
Last active January 6, 2024 09:47
Memoization with closures from FrontEndMasters algo course
const memoizedClosureTimes10 = () => {
let cache = {};
return (n) => {
if (n in cache) {
console.log(`Fetching from cache: ${n}`);
return cache[n];
} else {
console.log(`Calculating result~~`);
let result = n * 10;
@MarwanShehata
MarwanShehata / Memoiazation
Last active January 6, 2024 08:48
6. Basic Memoization Example from FrontEndMasters
const times10 = (num) => {
const a = num * 10;
return a;
};
console.log(`~~~~~~~~TASK 1~~~~~~~~`);
console.log(`times10 returns: ${times10(9)}`);
const cache = {};
const memoTimes10 = (n) => {
/* https://twitter.com/i/status/1553175201357221893 */
{
0% { opacity: 0; clip-path: inset(5%); transform: scale(111.11%) }
100% { opacity: 1; clip-path: inset(0); transform: scale(1) }
}
const style = +process.argv[2] || 0;
const count = +process.argv[3] || 1000;
const propCount = +process.argv[4] || 5;
class Foo2 {
constructor(a, b) {
if (a instanceof Foo2) {
this.myItem = a.myNumber * b % 42069;
} else {
this.myItem = a * b;
@MarwanShehata
MarwanShehata / Counter.js
Last active April 21, 2022 21:49
From 7 - Raising and Handling Events.mp4, Code With Mosh - Mastering React ![](https://cdn.cacher.io/attachments/u/3g08kb548w5j7/AlDuP4sNwhtsdRAU84CBLiHA1y13RO-M/rssmr7ja5.png)
/* Child Component */
import React, { Component } from "react";
class Counter extends Component {
render() {
const { onDelete } = this.props;
return (
<button
onClick={() => onDelete(counter.id)}
className="btn btn-danger btn-sm m-2"