Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Christina Zenzes & Anna Backs: How not to JavaS...

Christina Zenzes & Anna Backs: How not to JavaScript

How not to JavaScript – Anleitung zum Unglücklich sein (30 min)

Du hast ein Problem oder eine Idee und denkst darüber nach, das mit JavaScript zu lösen? Wir können dir zeigen, wie du das Projekt zu einer möglichst schmerzhaften und umständlichen Erfahrung machst! Wir haben alle unsere Fehler und Schwachstellen aus allen möglichen JavaScript-Projekten gesammelt und zu einem tollen Projekt zusammengeführt. Aber keine Sorge, wir haben überlebt und wollen nun mit dir teilen, was du besser nicht tun solltest...

Christina Zenzes

Christina Zenzes ist als Consultant und Software Developer bei der codecentric AG tätig. Ihre Leidenschaft gilt React, CSS, Clean Code und Testen (ja, wirklich!). Neben dem Schreiben von funktionellem JavaScript-Code liebt sie es, Pflanzen auf ihrem Balkon zu züchten, Katzen zu streicheln und gutes Essen.

Anna Backs

Anna Backs arbeitet als APM Consultant für die codecentric AG. Wenn sie nicht gerade mit ihrem Hund draußen unterwegs oder in der Welt von Shadowrun vertieft ist, dann ist sie wahrscheinlich damit beschäftigt, etwas in Java oder JavaScript zu bauen. Kaffee, gute Geschichten und neue Dinge zu lernen gehören zu ihren Vorlieben.

More Decks by SoftwerkerKonf - Der Softwerker als Konferenz

Other Decks in Technology

Transcript

  1. 5

  2. Warum macht mich das unglücklich? 10 Woher gedownloadet? Welche Version?

    Legitime Quelle? Weitere Abhängigkeiten? Mehrere Projekte?
  3. 28

  4. 30 Warum macht mich das unglücklich? class SadPerson { saySomething(text)

    { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion();
  5. 31 Warum macht mich das unglücklich? class SadPerson { saySomething(text)

    { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); Error: Uncaught [TypeError: this.thinking is not a function]
  6. answerQuestion() { thinking.call(this); … } 32 Warum macht mich das

    unglücklich? class SadPerson { saySomething(text) { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); This = ExecutionContext
  7. 33 Warum macht mich das unglücklich? class SadPerson { saySomething(text)

    { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); This = ExecutionContext setTimeout(callback, timeout) { //timer callback(); }
  8. 34 Warum macht mich das unglücklich? class SadPerson { saySomething(text)

    { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); This = ExecutionContext setTimeout(callback, timeout) { //timer callback.call(this); }
  9. 35 Warum macht mich das unglücklich? class SadPerson { saySomething(text)

    { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); This = ExecutionContext return setTimeout( this.answerQuestion, 2000 );
  10. 36 Warum macht mich das unglücklich? class SadPerson { saySomething(text)

    { console.log(text); } thinking() { console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); This = ExecutionContext return setTimeout.call( window, this.answerQuestion, 2000 );
  11. 37 class SadPerson { saySomething(text) { console.log(text); } thinking() {

    console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); Binding return setTimeout(this.answerQuestion.bind(this), 2000); Was sollte ich vermeiden?
  12. 38 class SadPerson { saySomething(text) { console.log(text); } thinking() {

    console.log("..."); } answerQuestion() { this.thinking(); this.saySomething("I’m sad because of this"); } askQuestion() { return setTimeout(this.answerQuestion, 2000); } } const sadPerson = new SadPerson(); await sadPerson.askQuestion(); return setTimeout(() => this.answerQuestion(), 2000); Arrow Function Was sollte ich vermeiden?
  13. Warum macht mich das unglücklich? 40 const reallyLongRunningMethod = ()

    =>{//do something long}; const shortRunnnigMethod = () => {//do something short} reallyLongRunningMethod(); shortRunnnigMethod(); EventLoop Global reallyLong RunningMethode do something long
  14. Warum macht mich das unglücklich? 41 const reallyLongRunningMethod = ()

    =>{//do something long}; const shortRunnnigMethod = () => {//do something short} reallyLongRunningMethod(); shortRunnnigMethod(); EventLoop Global
  15. Warum macht mich das unglücklich? 42 const reallyLongRunningMethod = ()

    =>{//do something long}; const shortRunnnigMethod = () => {//do something short} reallyLongRunningMethod(); shortRunnnigMethod(); EventLoop Global reallyLong RunningMethode do something long short RunningMethod do something short
  16. 43 EventLoop global short RunningMethod const reallyLongRunningMethod = callback =>

    { setTimeout(callback, 2000); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod(() => console.log("long")); shortRunnnigMethod(); browser api Was sollte ich vermeiden? reallyLong RunningMethod
  17. 44 EventLoop global short RunningMethod do something short const reallyLongRunningMethod

    = callback => { setTimeout(callback, 2000); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod(() => console.log("long")); shortRunnnigMethod(); browser api Was sollte ich vermeiden?
  18. 45 EventLoop global const reallyLongRunningMethod = callback => { setTimeout(callback,

    2000); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod(() => console.log("long")); shortRunnnigMethod(); queue callback browser api Was sollte ich vermeiden?
  19. 46 EventLoop global do something short const reallyLongRunningMethod = callback

    => { setTimeout(callback, 2000); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod(() => console.log("long")); shortRunnnigMethod(); queue callback console.log Was sollte ich vermeiden?
  20. 47 stack global reallyLong RunningMethod short RunningMethod browser api const

    reallyLongRunningMethod = () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 2000) ); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod() .then(text => console.log(text)); shortRunnnigMethod(); task queue Was sollte ich vermeiden?
  21. 48 stack global reallyLong RunningMethod browser api const reallyLongRunningMethod =

    () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 2000) ); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod() .then(text => console.log(text)); shortRunnnigMethod(); Was sollte ich vermeiden?
  22. 49 stack global micro task queue callback browser api const

    reallyLongRunningMethod = () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 2000) ); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod() .then(text => console.log(text)); shortRunnnigMethod(); task queue Was sollte ich vermeiden?
  23. 50 stack global do something short micro task queue callback

    console.log const reallyLongRunningMethod = () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 2000) ); }; const shortRunnnigMethod = () => { console.log("short"); }; reallyLongRunningMethod() .then(text => console.log(text)); shortRunnnigMethod(); task queue Was sollte ich vermeiden?
  24. 52 const getTextLineFromFile = callback => { setTimeout(() => {

    callback("long"); }, 1000); }; const formatText = (text, callback) => { setTimeout(() => { callback(`formatedText${text}`); }, 1000); }; const sendText = (text, callback) => { setTimeout(() => { callback("response text"); }, 1000); return; }; const printText = (text, callback) => { console.log(text); callback(text); }; getTextLineFromFile(text => formatText(text, text => sendText(text, text => formatText(text, text => printText(text)) ) ) ); Warum macht mich das unglücklich?
  25. 53 const getTextLineFromFile = callback => { setTimeout(() => {

    callback("long"); }, 1000); }; const formatText = (text, callback) => { setTimeout(() => { callback(`formatedText${text}`); }, 1000); }; const sendText = (text, callback) => { setTimeout(() => { callback("response text"); }, 1000); return; }; const printText = (text, callback) => { console.log(text); callback(text); }; getTextLineFromFile(text => formatText(text, text => sendText(text, text => formatText(text, text => printText(text)) ) ) ); Warum macht mich das unglücklich?
  26. 54 const getTextLineFromFile = callback => { setTimeout(() => {

    callback("long"); }, 1000); }; const formatText = (text, callback) => { setTimeout(() => { callback(`formatedText${text}`); }, 1000); }; const sendText = (text, callback) => { setTimeout(() => { callback("response text"); }, 1000); return; }; const printText = (text, callback) => { console.log(text); callback(text); }; const getTextLineFromFile = () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 1000) ); }; const formatText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve(`formatedText${text}`), 1000) ); }; const sendText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve("response text"), 1000) ); }; const printText = text => { return new Promise((resolve, reject) => { console.log("print Text", text); resolve(); }); }; Promises Warum macht mich das unglücklich?
  27. 55 const getTextLineFromFile = callback => { setTimeout(() => {

    callback("long"); }, 1000); }; const formatText = (text, callback) => { setTimeout(() => { callback(`formatedText${text}`); }, 1000); }; const sendText = (text, callback) => { setTimeout(() => { callback("response text"); }, 1000); return; }; const printText = (text, callback) => { console.log(text); callback(text); }; const getTextLineFromFile = () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 1000) ); }; const formatText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve(`formatedText${text}`), 1000) ); }; const sendText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve("response text"), 1000) ); }; const printText = text => { return new Promise((resolve, reject) => { console.log("print Text", text); resolve(); }); }; getTextLineFromFile().then(text => formatText(text).then(text => sendText(text).then(text => formatText(text).then(text => { return printText(text) .then(() => done()); }) ) ) ); Warum macht mich das unglücklich?
  28. 56 const getTextLineFromFile = callback => { setTimeout(() => {

    callback("long"); }, 1000); }; const formatText = (text, callback) => { setTimeout(() => { callback(`formatedText${text}`); }, 1000); }; const sendText = (text, callback) => { setTimeout(() => { callback("response text"); }, 1000); return; }; const printText = (text, callback) => { console.log(text); callback(text); }; const getTextLineFromFile = () => { return new Promise((resolve, reject) => setTimeout(() => resolve("long"), 1000) ); }; const formatText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve(`formatedText${text}`), 1000) ); }; const sendText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve("response text"), 1000) ); }; const printText = text => { return new Promise((resolve, reject) => { console.log("print Text", text); resolve(); }); }; getTextLineFromFile().then(text => formatText(text).then(text => sendText(text).then(text => formatText(text).then(text => { return printText(text) .then(() => done()); }) ) ) ); Warum macht mich das unglücklich?
  29. 57 const getTextLineFromFile = () => { return new Promise((resolve,

    reject) => setTimeout(() => resolve("long"), 1000) ); }; const formatText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve(`formatedText${text}`), 1000) ); }; const sendText = text => { return new Promise((resolve, reject) => setTimeout(() => resolve("response text"), 1000) ); }; const printText = text => { return new Promise((resolve, reject) => { console.log("print Text", text); resolve(); }); }; getTextLineFromFile().then(text => formatText(text).then(text => sendText(text).then(text => formatText(text).then(text => { return printText(text) .then(() => done()); }) ) ) ); getTextLineFromFile() .then(formatText) .then(sendText) .then(formatText) .then(printText) .then(() => done()); Was sollte ich vermeiden?
  30. 58

  31. 61 0.2 0 * 2 = 0.4 0.4 > 1

    ? 1 : 0 0 Warum macht mich das unglücklich?
  32. 62 0.2 0 * 2 = 0.4 0.4 * 2

    = 0.8 0 0.8 * 2 = 1.6 1 0.4 > 1 ? 1 : 0 If > 1, then -1 0 Warum macht mich das unglücklich?
  33. 63 0.2 0 * 2 = 0.4 0.4 * 2

    = 0.8 0 0.8 * 2 = 1.6 1 (1.6 -1) * 2 = 1.2 1 (1.2 -1) * 2 = 0.4 0 0.4 > 1 ? 1 : 0 If > 1, then -1 0 or …? 0.00110 Warum macht mich das unglücklich?
  34. 65

  35. 66 Typecasting Kein Typecasting false ToNumber(„1“) === ToNumber(true) „1“ ==

    true „1“ === true Warum macht mich das unglücklich?
  36. 67 Typecasting Kein Typecasting false 1 === 1 true „1“

    == true „1“ === true Warum macht mich das unglücklich?
  37. 68 Wenig & nur einfache Vergleiche schreiben Typecasting im Hinterkopf

    behalten Jobwechsel Was sollte ich vermeiden?
  38. 69