I learned few awesome javascript tricks this week
Structured clone
- Deep copy a java object, you can also transfer i.e detach object from one to another.
const superhero = {} superhero.dc = ["superman", "batman"]; superhero.marvel = ["captain","ironman"]; const superheroClone = structuredClone(superhero); superheroClone.marvel.push("spiderman"); console.log(superhero.marvel); // [ "captain", "ironman" ] console.log(superheroClone.marvel); // [ "captain", "ironman", "spiderman" ]
Event Listener
Scroll end
- I was looking similar to this for a week and found this listener.
- The listener will trigger for user as well as after javascript scrollTo.
ELEMENT.addEventListener("scrollend", (event) => { })
Offline and Online listener
- Listener to get trigger for offline and online functionality.
window.addEventListener("offline", (event) => { console.log("The network connection has been lost."); }); window.addEventListener("online", (event) => { console.log("You are now connected to the network."); });
Optional chaining
- This is super helpful (If you are using latest javascript).
object = null; object?.property // Undefined - no error will be thrown
- If the object is null, then we get undefined value not error thrown at runtime which is super cool
That's it. Thats all the things i learned during this week.