Skip to content

Instantly share code, notes, and snippets.

View ilearnjavascript's full-sized avatar

I Learn Javascript ilearnjavascript

View GitHub Profile
@ilearnjavascript
ilearnjavascript / enhanced-object-literals.js
Created March 28, 2019 23:49
Enahnced Object Literals
/* --- No repetition if key: value is identical --- */
// ES5
var es5_getLaptopProperties = function(make, model, year) {
return {
make: make,
model: model,
year: year
}
}
es5_getLaptopProperties("Apple", "MacBook", "2015");
@ilearnjavascript
ilearnjavascript / prototypal-inheritance-es5-es6-class.js
Last active March 28, 2019 23:41
Prototypal Inheritance in ES5 and ES6 (Class)
// ES5 - variant 1
var es5_Person = function (firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
this.greet = function () {
console.log('Hello I am ' + this.firstname + ' ' + this.lastname);
}
}
var es5_john = new es5_Person('John', 'Doe');
es5_john.greet(); // outputs: Hello my name is John Doe
@ilearnjavascript
ilearnjavascript / es6-rest-parameter.js
Created March 28, 2019 23:17
ES6: Rest Parameter
// Rest Parameter
const restParameter = (firstname, lastname, ...adress) => {
console.log('firstname: ', firstname);
console.log('lastname: ', lastname);
console.log('adress: ', adress);
}
restParameter('John', 'Doe', 'Example Street 1', '12345', 'Exampletown');
// outputs:
// firstname: John
// lastname: Doe
async function getJoke() {
let url = 'https://api.icndb.com/jokes/random';
let result = await (await fetch(url)).json();
return result;
}
async function initApiCall() {
try {
let data = await getJoke();
console.log(data.value.joke)
@ilearnjavascript
ilearnjavascript / callback-vs-promise-vs-async-await.js
Created March 28, 2019 23:05
callback-vs-promise-vs-async-await
// callback
function callback() {
setTimeout(function () {
console.log('1. First thing setting up second thing');
setTimeout(function () {
console.log('2. Second thing setting up third thing');
setTimeout(function () {
console.log('3. Third thing setting up fourth thing');
setTimeout(function () {
console.log('4. Fourth thing setting up fifth thing');
@ilearnjavascript
ilearnjavascript / plainjs - window.postmessage and iframes - two way communication.html
Created March 27, 2019 22:51
plainjs - window.postmessage and iframes - two way communication.html
<!-- window.html -->
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>window</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
@ilearnjavascript
ilearnjavascript / plainjs - window.postmessage and iframes - one way communication.html
Last active March 27, 2019 22:49
plainjs - window.postmessage and iframes - one way communication.html
<!-- window.html -->
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>window</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
@ilearnjavascript
ilearnjavascript / fadeout-plainjs.js
Last active March 25, 2019 00:04
fadeout-plainjs.js
const fadeOut = (el, smooth = true, displayStyle = 'none') => {
if (smooth) {
let opacity = el.style.opacity;
let request;
const animation = () => {
el.style.opacity = opacity -= 0.04;
if (opacity <= 0) {
opacity = 0;
el.style.display = displayStyle;
@ilearnjavascript
ilearnjavascript / fadein-plainjs.js
Created March 24, 2019 21:32
fadein-plainjs.js
const fadeIn = (el, smooth = true, displayStyle = 'block') => {
el.style.opacity = 0;
el.style.display = displayStyle;
if (smooth) {
let opacity = 0;
let request;
const animation = () => {
el.style.opacity = opacity += 0.04;
if (opacity >= 1) {