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

[CS Foundation] Web - 4 - JavaScript Web Course

[CS Foundation] Web - 4 - JavaScript Web Course

x-village

August 15, 2018
Tweet

More Decks by x-village

Other Decks in Programming

Transcript

  1. JavaScript /* Multi-line comments start with /* and end with

    */. Any text between /* and */ will be ignored by JavaScript. */ 多⾏行行註解
  2. 原始型態 有 5 種資料型別是原始型別: 1. 數字(Number):在 -(253 - 1) and

    253-1 之間的數字, Infinity, - Infinity, NaN 2. 字串串(String):代表⽂文字資料 3. 布林林(Boolean):true 與 false。 4. 未定義(Undefined):沒有被定義的變數 undefined 5. Null:只有⼀一個值:null (意味著值不能被改變)
  3. JavaScript var num = 12; var str = 'Mark'; var

    bool = true; var undef; // undefined var empty = null; “undefined” ⽤用來來表⽰示並沒有被賦予值 “null” 相當於 Python 中的 'None' 原始型態 程式碼
  4. JavaScript 物件 (object) var obj = { key: 'value' };

    Python dictionary = { 'key': 'value' } 這裡的兩兩者的對比僅是概念念上的類似,並非完全相同。
  5. JavaScript 陣列列 array var arr = [1, 2, 3, 4,

    5]; Python list1 = [1, 2, 3, 4, 5] 這裡的兩兩者的對比僅是概念念上的類似,並非完全相同。
  6. JavaScript 1 + 3 // 4 var sum = 1

    + 3; Python # 4 sum = 1 + 3
  7. JavaScript 但是JS 運算允許不同型態運算 // '13' var numPlusStr = 1 +

    '3'; Python # TypeError num_plus_str = 1 + '3' 需要特別⼩小⼼心不同型態之間的運算
  8. JavaScript 需有⼤大括號・else if vs elif if(grade > 87) { console.log('不能再⾼高');

    } else if (grade > 60) { console.log('及格'); } else { console.log('不及格'); } Python if grade > 87: print('不能再⾼高') elif grade > 60: print('及格') else: print('不及格')
  9. JavaScript 使⽤用“&&”與“||”取代 and 與 or if (age > 20 &&

    age < 65) { console.log('work hard'); } Python if age > 20 and age < 65: print('work hard')
  10. JavaScript “==”vs“===” // true console.log(1 == '1'); // false console.log(1

    === '1'); 推薦使⽤用三個等號,避免型態⾃自動轉換 兩兩個等號會⾃自動將兩兩邊轉換相同型態比較
  11. JavaScript while 需有⼤大⼩小括號 // plus from 1 to 10 var

    num = 1; while(num <= 10) { num += 1; } Python # plus from 1 to 10 num = 1 while num <= 10: num += 1
  12. JavaScript “for” 沒有 “range” // 印出 0…9 for(var i =

    0; i < 10; i++) { console.log(i); } Python // 印出 0…9 for i in range(10): print(i)
  13. 解析 for 迴圈 for(var i = 0; i < 10;

    i++) { // i++ 等於 i+=1 console.log(i); } 初始值 終⽌止條件 每⼀一次迴圈後執⾏行行
  14. ⼀一樣有 break 跟 continue for(var I = 0; I <

    10; I++) { console.log(i); break; } JavaScript
  15. JavaScript try…catch… try { sum(); } catch(err) { console.log(err); }

    Python try: sum() except Exception as err: print(err)
  16. JavaScript var shoppingList = ['Milk', 'Bread', 'Beans']; // 3 shoppingList.length;

    // ['Milk', 'Bread', 'Beans', 'A new car'] shoppingList.push('A new car'); // ['Milk', 'Bread', 'Beans'] shoppingList.pop(); length, push(), pop()
  17. JavaScript var iterable = [10, 20, 30]; for (var i

    = 0; i < iterable.length; i++) { console.log(iterable[i]); } // 10 // 20 // 30 for 列列舉陣列列裡⾯面的元素
  18. JavaScript var iterable = [10, 20, 30]; for (var value

    of iterable) { console.log(value); } // 10 // 20 // 30 也可以使⽤用 “for of” 列列舉
  19. JavaScript var jedi = { name: "Yoda", age: 899, talk:

    function () { alert("another... Sky... walk..."); } }; // Yoda console.log(jedi.name); // 899 console.log(jedi['age']); // produces an alert box console.log(jedi.talk()); // And add new ones on the fly jedi.lightsaber = "purple"; 物件可以存放任何東⻄西
  20. JavaScript var obj = { a: 1, b: 2, c:

    3, }; //['a', 'b', 'c'] console.log(Object.keys(obj)); Object.keys() 取得物件裡的 keys
  21. JavaScript var obj = { a: 1, b: 2, c:

    3, }; // true console.log(obj.hasOwnProperty('a')); // false console.log(obj.hasOwnProperty('d')); 查詢 hasOwnProperty 物件是否有該 Property
  22. JavaScript var obj = { a: '1', b: '2', };

    for (var key of Object.keys(obj)) { console.log(key, obj[key]); } // a 1 // b 2 “for of” 列列舉物件裡⾯面的元素
  23. JavaScript Hoisting:兩兩者其實是⼀一樣的 x = 5; // Assign 5 to x

    var x; // Declare x JavaScript var x; // Declare x x = 5; // Assign 5 to x
  24. JavaScript ES2015 版本新增了了 let & const 的變數宣告⽅方法 const x =

    1; let y = 1; 兩兩者都不受作⽤用域提升 (Hoisting)影響 讓變數除了了 Global Scope, Function Scope 還多了了 Block Scope
  25. JavaScript const 宣告後不能重新 assign 值 const x = 1; //

    Uncaught TypeError: Assignment to constant variable. x = 2; const point = {}; // 注意:如果只改變該變數裡⾯面的值是可以的 point.x = 1; // { x: 1 }
  26. JavaScript 更更多找尋⽅方法 // Find an element by element id document.getElementById('button1');

    // Find elements by tag name document.getElementsByTagName('H1'); // Find elements by class name document.getElementsByClassName('demo-class'); // Find a element that matches a specified CSS selector document.querySelector("#header") // Find all elements that matches a specified CSS selector document.querySelectorAll(".intro")
  27. JavaScript addEventListener() // Add an event listener that fires //

    when a user clicks a button: document.getElementById("myBtn").addEventListener( "click", myFunction ); function myFunction() { console.log("Hello World!"); }
  28. JavaScript addEventListener() // Add an event listener that fires //

    when a user clicks a button: document.getElementById("myBtn").addEventListener( "click", myFunction ); function myFunction() { console.log("Hello World!"); } Callback
  29. JavaScript .innerHTML & setAttribute(…) // Change the inner HTML of

    an element document.getElementById("demo").innerHTML = "<h1>Hello World!</h1>"; // Change the attribute value of an HTML element document.getElementById("img1").setAttribute('src', 'http...');
  30. JavaScript 更更改 id = demo 的元素顏⾊色為紅⾊色 // Change the style

    of an HTML element document.getElementById("demo").style.color = 'red';
  31. JavaScript .classList // Add specified class values. document.getElementById("demo").classList.add('new-class'); // Remove

    specified class values. document.getElementById("demo").classList.remove('old-class');
  32. JavaScript 從 server 讀取資料(先看過就好) // The function requests data from

    a web server // and displays it function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); }
  33. JavaScript 選擇 HTML 元素 // Select all <p> elements on

    a page var $p = $("p"); // Find elements with a specific class var $intro = $(".intro");
  34. JavaScript 新增 HTML // Inserts content at the end of

    the selected elements $("p").append("Some appended text."); // Inserts content at the beginning of the selected elements $("p").prepend("Some prepended text.");
  35. JavaScript 移除/清空 HTML // Removes the selected element(s) and its

    child elements $("#div1").remove(); // Removes the child elements of the selected element $("#div1").empty();
  36. JavaScript POST 資料到 Server var postData = { key: 'value'

    }; $.post( “/save”, postData, function(data) { console.log('success'); });
  37. 延伸閱讀 • Make a Website • Web Developer Roadmap •

    Eloquent JavaScript • You Don’t Know JS 系列列