Dzień 1 - Javascript wprowadzenie
by Kemal Erdem | @burnpiro
Łatwość nauki
Kompatybilność wsteczna
Dowolność paradygmatów
DOM - Document Object Model
CSSOM - CSS Object Model
Web APIs - cała reszta technologii
Interakcje użytkownika z twoją aplikacją
interface Event {
constructor(DOMString type, optional EventInit eventInitDict = {});
void stopPropagation();
void stopImmediatePropagation();
void preventDefault();
readonly attribute DOMString type;
readonly attribute EventTarget? target;
readonly attribute EventTarget? currentTarget;
readonly attribute unsigned short eventPhase;
readonly attribute boolean bubbles;
readonly attribute boolean cancelable;
readonly attribute boolean defaultPrevented;
readonly attribute boolean composed;
}
void addEventListener(string type, function callback, options = {});
const button = document.getElementById('dataSubmitter');
button.addEventListener('click', e => {
console.log('clicked');
});
button.addEventListener('click', e => {
console.log('clicked second');
});
const button = document.getElementById('dataSubmitter');
button.addEventListener('click', e => {
console.log(e.target.className);
console.log(e.target.dataset.time);
});
document.myForm.addEventListener('submit', e => {
e.preventDefault();
e.stopPropagation();
console.log(e.target.elements["firstName"]);
console.log(e.target.elements["lastName"]);
console.log(e.target.elements["address"]);
});
localStorage.setItem("importantData", JSON.stringify(examQuestions));
localStorage.setItem("importantData", JSON.stringify(examQuestions));
const importantData = localStorage.getItem("importantData");
return JSON.parse(importantData);
sessionStorage.setItem("importantData", JSON.stringify(examQuestions));
const importantData = sessionStorage.getItem("importantData");
return JSON.parse(importantData);
const importantData = sessionStorage.getItem("importantData");
return JSON.parse(importantData); // Error!!!!!
Uncaught SyntaxError: Unexpected token u in JSON at position 0
let myHeight = 50
myHeight = 150
const myHeight = 50
myHeight = 150 //Uncaught TypeError: Assignment to constant variable.
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData); // "Different Data"
console.log(topScope); // `I'm Sitting on Top of the World`
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData); // "Some internal data I want to keep for myself"
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff(innerData = "Different Data") {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(topScope);
}
function doSthElse() {
// Second level scope
console.log(innerData);
}
doStaff();
doSthElse();
}
main();
console.log(myStaff); // ReferenceError: myStaff is not defined
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(myStaff);
console.log(topScope);
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(myStaff);
console.log(topScope);
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(myStaff);
console.log(topScope);
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(myStaff);
console.log(topScope);
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData);
console.log(myStaff);
console.log(topScope);
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData); // "Some internal data I want to keep for myself"
console.log(myStaff); // "can't touch this"
console.log(topScope); // "I'm Sitting on Top of the World"
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
let topScope = `I'm Sitting on Top of the World`;
function main() {
// First level scope
const innerData = "Some internal data I want to keep for myself";
function doStaff() {
// Second level scope
let myStaff = `can't touch this`;
console.log(innerData); // "Some internal data I want to keep for myself"
console.log(myStaff); // "can't touch this"
console.log(topScope); // "I'm Sitting on Top of the World"
}
return {
doStaff
};
}
const myClousure = main();
let innerData = "outer scope";
let myStaff = "outer scope";
myClousure.doStaff();
console.log(myStaff); // "outer scope"
"Nie ma głupich pytań, są tylko te niezadane!"
Kemal Erdem | @burnpiro
https://erdem.pl
https://github.com/burnpiro