Download Server-Side Development Pt.1 - Department of Computer Science...
Description
CSCI 3100 Tutorial 3 JavaScript & Node.js Presented by SU Yuxin Department of Computer Science and Engineering The Chinese University of Hong Kong
1
Outline • • • • •
Introduction to JavaScript JavaScript Basic Advanced Features of JavaScript Introduction to Node.js A Secret…
2
Do you know JavaScript? Java
JavaScript
3
What is JavaScript Script Language
• Shell • C#
Dynamic Type
• Python • Java
Objectedoriented
• C++ •C Unique Feature:
4
Where to use JavaScript • The whole picture: browser
modify/create/delete data transmit
HTML CSS JavaScript HTML5
Server Side
• PHP • Rails on Ruby • JavaScript ??? 5
JavaScript Basic • • • • •
Output Variable & Arithmetic Object & Array Function IF & Loop statement
6
JavaScript Basic Output • No printf()! • Use console.log() / alert() instead.
7
JavaScript Basic Variable & Arithmetic Number can be integer or floating point String can be in single or double quotes
If there is no initial value, the value is undefined
String concatenation
Integer Arithmetic 8
JavaScript Basic Object
You can use any property without declaration.
You can also treat function as a property of the object, and assign it like this. 9
JavaScript Basic Array Create an array Initialization Access an array Use it as a dictionary Multi-type
Build-in functions and properties
10
JavaScript Basic Function Declaration
Argument & Return value
11
JavaScript Basic IF & Loop statement
FOR Loop
IF statement
WHILE Loop Note: Anything defined will be treat as true. Undefined will be treat as false. 12
ADVANCED FEATURES
13
Regular Expression • First-class citizen in JavaScript • Create: var pattern = /g$/; Any expression ended with letter ‘g’ var pattern = new RegExp(“g$"); • Use: "Software Engineering".search(pattern); text.replace(/javascript/gi, "JavaScript"); "1 plus 2 equals 3".match(/\d+/g); "123,456,789".split(","); 14
Prototype • function Range(from, to) { It looks like the definition this.from = from; this.to = to; of class! } Range.prototype = { includes: function(x) { return this.from