Skip to content

Instantly share code, notes, and snippets.

@KLagdani
KLagdani / callStackExample2.js
Created June 16, 2020 12:20
Call Stack example 2
function attack(attacker) {
function getAttackName() {
return "Kamehameha";
}
var attackName = getAttackName();
console.log(attacker + " throws " + attackName);
}
attack("Goku");
@KLagdani
KLagdani / callStackExample.js
Created June 16, 2020 12:17
Call Stack example
function getAttackName() {
return "Kamehameha";
}
function attack(attacker) {
var attackName = getAttackName();
console.log(attacker + " throws " + attackName);
}
attack("Goku");
@KLagdani
KLagdani / badGlobalVar.js
Created June 16, 2020 12:12
Using wrongly or too many global variables
const attackWarning = " is attacking you!";
function attack(attacker) {
const attackResult = "You dead man";
console.log(attacker + attackWarning );
console.log(attackResult);
}
attack("Goku");
@KLagdani
KLagdani / badTimers.js
Created June 16, 2020 12:11
Setting timers and forgetting them
setInterval(() => {
var mySaiyan = document.getElementById("saiyan");
if (typeof(mySaiyan) != 'undefined' && mySaiyan != null) {
//Do some heavy load actions
console.log("My saiyan exists!");
}
}, 1000);
@KLagdani
KLagdani / functions-example2.js
Created May 19, 2020 12:06
Functions example 2
function attack(attacker) {
return function(attackName) {
console.log(attacker + " throws a " + attackName);
};
}
var throwAttack = attack("Son Goku");
throwAttack("Kamehameha");
@KLagdani
KLagdani / functions-example.js
Created May 19, 2020 11:54
Functions example
var multiplyByTen = function(num) {
return num * 10;
};
function mapArray(arr, fn) {
var resultingArr = [];
for (var i = 0; i < arr.length; i++) {
resultingArr.push(fn(arr[i]));
}
return resultingArr;
@KLagdani
KLagdani / function-as-object.js
Created May 19, 2020 11:49
In JavaScript functions are objects
function attack() {
console.log("You have been attacked!");
}
attack.newProperty = "holololo";
console.log(attack.newProperty); //holololo
@KLagdani
KLagdani / object.js
Created May 19, 2020 11:45
A simple object in JavaScript
var saiyan = {
firstName: "Son Goku",
attackName: "Kamehameha",
address: {
street: "439 East District",
city: "Paozu"
}
};
@KLagdani
KLagdani / function-statement.js
Created May 6, 2020 11:53
Hoisting and function statement
attack("Son Goku");
function attack(who) {
console.log(who + " attacked you!");
}
@KLagdani
KLagdani / function-expression.js
Created May 6, 2020 11:52
Function expression and hoisting
attack("Son Goku");
var attack = function(who) {
console.log(who + " attacked you!");
};