close

ATOM

What is JavaScript?
  - A little script to run interactive changes in CSS and HTLM documents.
image

How does JavaScript relate to Java? No relation

Navigating the Javascript landscape : EMCAScript

Tools :
image
  - Code text : Atom
  - Browsers : Crowme

==== The Basic ====

Browser console : only execute on the fly, not saved in the local file
  - ctrl-shift+i
image
image
image

Documents are read by order in HTLM

Add JavaScript in an external file
image
image
image
image
image
image

How to write a good JS
  1. JS is Case Sensitive
  2. Use camelCase
    i. Variables start with lc
    ii. Objects and classes start with uc
    iii. Constans are all-caps
  3. Whitespace Matters (to Humans)
  4. End Each Statement with Semicolon
  5. Use Comments Liberally
    i. //
    ii. /* ... */

Variables: The catch-all container of Javascript
  - var <var name>;
  - Data Type
image
image
  - Use "+" to concate strings
  - Logical Operations
image
image
  - Array
image
    - Operations
      - Length : <array name>.length
      - Reverse; : <array name>.reverse()
      - Remove the first value : <array name>.shift()
      - Add element to the front : <array name>.unshift("<1st>", "<2nd>", ...)
      - Remove the last item : <array name>.pop()
      - Add elements to the last : <array name>.push("<1st>", "<2nd>", ...)
      - Remove elements in the middle : <array name>.splice(pos, n), means specify the pos position, and remove the num after pos
        - a = [1, 2, 3, 4] --> a.splice(2, 1) --> a = [1, 2, 4]
      - Create a copy from an array : var <new array> = <array name>.slice();
      - Search : var result = <array name>.indexOf("<key word>", start index)
      - Join : <array name>.join("<connector>")

Functions : 
image
  - Initialize function :
image

  - New Types : const and let
image
image

  - Two ways to declare functions
image

Arrow function :

image

  - Arrow funciton and normal function will have different scope, if both functions return different values, something wrong
image
image

  - callback function
    - add callback into arguments
    - pass a function into a function for local usage

  - Conditional switch statement
    - switch (expr) {
         case <state a>:
         case <state b>:
         ....
         default :
       }

image

Class :
imageimage
  - Declare an object/declare by constructor
imageimage

  - Classes Array
image
  - Dot and Bracket notation
image
  - Closure :
image

Constructor :
imageimage

Use function to declare class itself as a constructor
image

3 ways to create class
  1. class <class name> {
        construct (aa, bb, ...) {
          this.aa = aa;
          this.bb = bb;
        }
      }
  2. const <class name> = {
        aa : value_a,
        bb : value_b,
      };
  3. function <class name> (aa, bb, ...) {
        this.aa = aa;
        this.bb = bb;
      }

==== JavaScript and DOM  ====

document.body : the whole body
  - document.body.innerHTML : the content

DOM : The document object model
image
image
image

Target elements in the DOM
  - https://developer.mozilla.org/en-US/docs/Web/API
  - DOM Properties/Methods
imageimage
image

CSS Query : name/class

  - document.querySelector(".masthead") : For the first selector
  - document.querySelector(".menu .has-children a")
  - document.querySelectorAll("a") : For all selectors
  - document.querySelectorAll(".social-nav a[href*='linkedin.com'])
  - document.querySelector("main li:last-child")
  - document.querySelector("main li")
    - document.querySelectorAll("main li").forEach(item => item.style.backgroundColor = "red");
    - document.querySelectorAll("main li:last-child").forEach(item => item.style.backgroundColor = "grey");
  
Access and change elements
  - https://developer.mozilla.org/en-US/docs/web/API/element
    - document.querySelector(".main-title").innerHTML
    - document.querySelector(".main-title").outerHTML
    - document.querySelector("#showcase").id = "slideshow"
image
    - Class
      - document.querySelector(".masthead")
      - document.querySelector(".masthead").className
      - document.querySelector(".masthead").classList
image

Access and change classes : for read only elements
  - add
image
  - Find elements : Old, use querySelector(All) is better
    - Use document.getElementByClassName
    - Use document.getElementByID

**  Shouldn't use className property in framework to avoid unexpected problems

Modifying element classes
  - document.querySelector().className
image

**  classList is much better instead of className

  - document.querySelector().classList
    - add/remove/toggle/replace/

Attributes
  - document.querySelector().attributes
  - document.querySelector().hasAttribute() : to check attribute exists or not
  - document.querySelector().getAttribute() : 
  - document.querySelector().setAttribute() : 
    - to add value to an attribute
    - to add a new attribute
  - document.querySelector().removeAttribute() : 

Inline Style :
image
image

image

Add DOM elements

**  Create a new article appended to main, not replace the original one (05_09)

  - document.createElement()
    - Create HTLM element like "<a></a>", if document.createElement("a")
  - ParentNode.append()
image
image

When we use JavaScript to create backpack element, the main content will be replaced. So add the backpack inside the main content, after the article, page-header.

image
image

  - Element.insertAdjacetElement()

Exercise : Add Menu decorated by CSS
imageimage

  - list-style-type : head symbol
  - margin : outer distance to the frame, link
  - padding : inner distance to the fram, link
  - display : display behavior, link
  - justify-content : alignment, link
  - color : link

==== Sidebar: Variables and Data Types ====
- Containers for everything

==== Events ====

Event Listener :
  - target.addEventListener(target, callback, [, options])

image
imageimage

this (09_05), call back function :
image

Pass arguments through event listeners : Pass arguments into callback function
  - 09_06, common pattern

Loop : 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

image
image
image
image

Map : Used to create a new array
image

Debuggin 
  - Use "source"
  - F9 step

arrow
arrow
    全站熱搜

    pixnetinpenang 發表在 痞客邦 留言(0) 人氣()