Skip to content

Instantly share code, notes, and snippets.

View szaranger's full-sized avatar

Sean Amarasinghe szaranger

View GitHub Profile
const element = document.getElementById('button');
function onClick(event) {
element.innerHtml = 'text';
}
element.addEventListener('click', onClick);
// Do stuff
// ...
// Remove handlers
const data = getData();
setInterval(function() {
const node = document.getElementById('id');
if(node) {
// Do stuff with node and data.
node.innerHTML = JSON.stringify(data));
}
}, 1000);
let original = null;
const duplicate = function () {
const isOriginal = original;
const unused = function () {
if (isOriginal) {
console.log("I'm unused");
}
};
function outer() {
const obj = {
id: 1,
name: "David",
};
function closure() {
const { name } = obj;
sayHello(name);
}
}
// This will also become a global variable as arrow functions
// do not have a contextual `this` and instead use a lexical `this`
const hey = () => {
this.foo = "Hello world!";
}
// This will also become a global variable as global functions have
// global `this` as the contextual `this` in non-strict mode
function hey() {
this.foo = "Hello world!";
}
// This will be hoisted as a global variable
function hey() {
foo = "Hello world!";
}
fucntion Person(name, dob) {
this.name = name;
this.dob = dob;
}
const sherlock = new Person({
name: 'Sherlock Holmes',
dob: '01-06-1852';
});
fucntion Person(name, dob) {
this.name = name;
this.dob = dob;
}
const john = new Person({
name: 'Dr John Dollar',
dob: '01-08-1866'
});
import useSWR from 'swr';
const Post = () => {
const { data, error } = useSWR('/api/post/123', fetch);
if (error) {
return <div>Error loading post!</div>;
}
if (!data) {