Javascript
Bring things to live
Week 4 Theory (Cont’d)
Playground Services
Examples:
http://codepen.io/patterns -- A collection of code snippets for design patterns
How to ask questions
Ref:
http://stackoverflow.com/help/how-to-ask
[important] A code generation trick
Use spreadsheet to save duplicated work:
https://docs.google.com/spreadsheets/d/1vCnb_-W0T6F8iSkZGH0VdiqbM7-ICq0ZGzhAFslNobI/edit#gid=0
(Can use javascript to save duplicated work in the future)
Concept Review
How it all fit together?
HTML: Element
Light
Mirror
Glass
Engine
Wheel
CSS: Style
Shell: Blue;
Light: White;
Size: 4.9m X 1.9m X 1.4m;
Glass: Transparent;
...
JS: Control
Light:
on/off
Engine:
start/ stop/ speed up/ speed down
Wheel:
turn
left/ right
Paired and nested tags
html, head, body, h1, h2, p, b, i, ul, ol, li, img
...
Different element → different key
Different key → different value set
Selector
Key:Value;
Can manipulate HTML/CSS
Context matters
Can only touch the surface here
document.getElementById('myp').style='background-color:lightgrey;color: green;'
document.getElementById('myp').innerHTML = '<ul><li>item1</li><li>item2</li></ul>'
Result
JS
The missing pieces:
Javascript
Basic Operations
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Math
Math operation with variables
Data Types
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables
Use Functions
Syntax: object.function(arg1, arg2, ...)
alert("Hello World")
window.alert("Hello World")
Tip: Learning Javascript libraries are mainly about learning what are the available objects, available functions and how to use them. -- Object Oriented Programming
String and String operations
Commonly used objects
Note: Date.now() gives a “unixtimestamp”, see here what it is: http://www.unixtimestamp.com/
DOM Object
Javascript in the Console
New Object
d = new Date()
d.getMonth()
d.getHours()
Further reading:
http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript
It can be confusing for the “new” operation. Sometimes you need to use “new”. Sometimes you call the function directly. Just follow documentation or samples at this stage.
Define function
function(arg1, arg2, ...){
// Your code blocks here
return … // return value
}
Control flow
Control flow determines the order of code execution.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements
if...else
for...in
Integrated Exercise for Javascript
Topic:
Sample page:
jQuery
This part is postponed to next week. The slides are here for completeness. You may search the terms/ pointers mentioned in the slides if interested.
Background of jQuery
Key concepts
Example 1: Make a calculator
Example 1 breakdown
Example 1 breakdown
Any codes here. This code block is executed after all HTML elements are loaded on this page. For now, just follow this common practice.
Example 1 breakdown
Example 1 breakdown
Example 2: Toggle style
Example 2: Toggle style - Key points
Example 3: A moving bar chart
Homework 4
Topic (template) for selection:
Logistics:
Tech requirements:
This part is postponed to next week. The slides are here for completeness. You may search the terms/ pointers mentioned in the slides if interested.
Guest Lecture
Cédric Sam - Interactive Graphics Journalist
Cédric Sam will visit the campus of HKBU and share with the audience a few hand-picked interactive web stories during the talk and outline the end-to-end workflow that takes those stories to life.
I work at Bloomberg News (Graphics) since December 2015. Previously, I worked at the South China Morning
Post (2013-15), La Presse (2013), JMSC HKU (2010-2012) and CBC/Radio-Canada (2007-09). Proud Montrealer
now living in New York City, after seven years in Hong Kong. [Electric Rice Cooker Tumblr | @cedricsam]
Slides:
Common Questions
Chrome Developer Console
The JavaScript Console provides two primary functions for developers testing web pages and applications. It is a place to:
Reference: https://developer.chrome.com/devtools#console
Java v.s. Javascript
Basically, They have NO RELATIONSHIP at all!!
Two almost totally unrelated languages. JavaScript was named this way mostly because of marketing reasons: at the time, Java was the new cool kid in town, so Netscape (Netscape had lots of weight to throw around at the time) named their scripting language "JavaScript" mostly to get up on the bandwagon.
Reference:https://www.quora.com/JavaScript-vs-Java
Screenshots of lecture demo �(Chrome Developer Console)
Screenshots of lecture demo �(Chrome Developer Console)
Screenshots of lecture demo �(Chrome Developer Console)
Screenshots of lecture demo �(Chrome Developer Console)