CS5220 Advanced Topics in Web Programming Node.js Basics

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introducing JavaScript
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Java Script Session1 INTRODUCTION.
The Web Warrior Guide to Web Design Technologies
CSCI 3100 Tutorial 3 JavaScript & Node.js Presented by SU Yuxin Department of Computer Science and Engineering The Chinese University of Hong Kong 1.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Tutorial 10 Programming with JavaScript
JAVASCRIPT Introduction Kenny Lam. What is Javascript?  Client-side scripting language that can manipulate elements in the DOM  Event-driven language.
Taking JavaScript Seriously IS NOT THE WORST IDEA.
JavaScript JavaScript is a scripting language that is most commonly used to add client- side programming to a web page. Some of the things it is used for.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
JavaScript davide morelli history LiveScript (1995) in Netscape java script is a misleading name started as a scripting counterpart.
Introduction to JavaScript Gordon Tian
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
“The world’s most misunderstood language has become the world’s most popular programming language” Akshay Arora
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript for C# developers Dhananjay Microsoft MVP
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Click to add Text Javascript Presented by Bunlong VAN Basic.
CGS 3066: Web Programming and Design Spring 2017
JavaScript Introduction
Chapter 10 Programming Fundamentals with JavaScript
Web Systems & Technologies
“Under the hood”: Angry Birds Maze
JavaScript: Good Practices
Tutorial 10 Programming with JavaScript
Chapter 4 Client-Side Programming: the JavaScript Language
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
Game-Changing Features in ES2015
JAVASCRIPT INTERVIEW QUESTIONS & ANSWERS.
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
JavaScript.
Functional Programming with Java
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
Web Systems Development (CSC-215)
Chapter 10 Programming Fundamentals with JavaScript
PHP.
CS5220 Advanced Topics in Web Programming JavaScript Basics
University of Kurdistan
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Week 01 Node.js Week 01
Tutorial 10 Programming with JavaScript
CS3220 Web and Internet Programming JavaScript Basics
CS3220 Web and Internet Programming Expression Language (EL)
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS5220 Advanced Topics in Web Programming More Node.js
JavaScript CS 4640 Programming Languages for Web Applications
CS3220 Web and Internet Programming Expression Language (EL)
CS5220 Advanced Topics in Web Programming Angular – TypeScript
Common Lisp II.
CS3220 Web and Internet Programming JavaScript Basics
CS5220 Advanced Topics in Web Programming More Node.js
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Chengyu Sun California State University, Los Angeles
JavaScript CS 4640 Programming Languages for Web Applications
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS5220 Advanced Topics in Web Programming Node.js Basics Chengyu Sun California State University, Los Angeles

About JavaScript Originally developed by Netscape Standardized as ECMAScript Three main variations: Client-side JavaScript, Node.js, and TypeScript Year Version 1997 1 2009 5 (ES5) 2015 6 (ES6 or ES 2015) 2016 7

Client-Side JavaScript JavaScript inside browser THE language for client-side web development ES5 and partially ES6 Browser objects like window and document; DOM HTML 5 APIs jQuery

Node.js Standalone JavaScript Popular for server-side web development and used even for desktop applications Mostly ES6 (built on Chrome’s V8 JavaScript engine) Additional language features like a new runtime environment and support for modules Many libraries

TypeScript JavaScript with Type Makes JavaScript more suitable for large software projects Static type checking, improved tool support … ES6 and beyond (compiled to plain JavaScript) Used in Angular applications Type and more

Node.js Development Tools npm package manager Node Shell, a.k.a. Node REPL (Read-Eval-Print-Loop) Text editors for developers Sublime Text Visual Studio Code by Microsoft Atom by GitHub

Basic Usage of NPM npm init npm install [-g] <package> Local vs global npm update [-g] Semantic versioning npm uninstall [-g] <package> More on NPM

Recommended NPM Global Packages typescript and ts-node for running TypeScript outside VS Code @angular/cli for creating Angular projects express-generator for creating Express projects nodemon for running Node.js server applications

Recommended VS Code Extensions ESLint and TSLint for integrating eslint and tslint with VS Code Prettier for code formatting Code Runner for running code directly in VS Code Settings Sync if you want to synchronize VS Code settings on several computers

Elements of an Imperative Programming Language Comments Types Literals and Variables Operators and expressions Statements Functions Classes and Objects Packages

Elements of JavaScript Comments Types Literals and Variables Operators and expressions Statements Functions Classes and Objects Packages

The Strict Mode Change some default JavaScript behaviors to make it easier to spot errors, optimize performance, and migrate to future versions of ES Put "use strict"; (or 'use strict';) before any statement in a script

Comments Single-line comment: // Block comment: /* */

Types Boolean Number String Null Undefined (Symbol) Object Primitive Types

Literals Boolean: true, false Number: 123, 4.56 String: "hello", 'world' Null and Undefined: null, undefined Template literal Object literal

Variables and Constants let x; // declare a variable x x = 10; // now x is a number x = 'abc'; // now x is a string const y = 20; // y is a constant JavaScript variables are dynamically typed (think of them as references instead of storage spaces)

Variable Scope Scope example Avoid using var or global variables a = 10; // global scope var b = 20; // function scope let c = 30; // block scope const d = 40; // block scope Scope example Global vs function vs block Strict vs non-strict mode Avoid using var or global variables

Template Literal A.K.A. Template String Example: let a = 10; let b = 20; console.log( `${a}+${b} is ${a+b}` ); Expression

Valid JavaScript Identifier Object Literal Valid JavaScript Identifier (Variable Name) { make: 'Honda', model: "Civic", "Year": 2001, 'owner': { name: "Chengyu" } String or Number An object literal consists of zero or more key:value pairs called properties

JSON (JavaScript Object Notation) Used as a data exchange format Based on a subset of JavaScript syntax Strings are double quoted Property keys are strings { "make": "Honda", "model": "Civic", "year": 2001, "owner": { "name": "Chengyu" }

Valid JavaScript Identifier Object Property Valid JavaScript Identifier (Variable Name) String or Number console.log( obj.make ); console.log( obj["make"] ); obj[0] = 10; Properties can be dynamically added What if there’s a variable make?  Computed Property

Property Value Shorthand let name = 'Joe'; let user = { name: name, age: 20 } let name = 'Joe'; let user = { name, age: 20 } Often used in object initializers, e.g. function createUser(name, age) { return {name, age}; }

Array a = ["x", "y", "z"]; a.b = "hello"; a[100] = 10; { 0: "x", 1: "y", 2: "z" } An array is a special object where array elements are stored as object properties An array may have “holes” (i.e. undefined elements) Array has built-in properties like length

Operators All Java operators, e.g. +, -, =, && … Strict equality/inequality: ===, !== === true if same type and same value Type operators: typeof, instanceof Object property operators: in, delete

Automatic Type Conversion 2 + 4/2 2 + 3/2 "2" + 3/2 3/2 + "2" 3/2 * "2" 3/2 + "two" 3/2 * "two" 0 == false "" == false 0 == "" null == false undefined == false ! null == true ! undefined == true Pros and cons of automatic type conversion??

Statements All common Java statements, e.g. if, for, while, switch, break, continue … for..in loop iterate over object property keys Strict equality check is used in switch statement Semicolon is optional but recommended

Functions as First-class Citizens In JavaScript, functions are actually objects Assigned to a variable Assigned as a property of an object Function literals (a.k.a. function expressions, anonymous functions) Passed as a function argument Returned as a function result

Function Examples function foo() { Regular function alert("foo"); } bar = function() { alert("bar"); }; setTimeout( bar, 5000 ); setTimeout( function() { alert("foobar");}, 5000 ) Regular function declaration Function literal Function assignment Function as parameter Function literal as parameter

Function Arguments function add(x,y) { return x+y; } add(10,20); add("10","20"); add(10); add(10,20,30); A special variable arguments hold all the arguments to a function arguments is not an array but similar to an array, e.g. arguments.length, arguments[0], arguments[1], …

Arrow Functions A.K.A. lambda expressions, lambdas A more concise way to write function literals Does not have its own this or arguments variables function(a) { return a*2 } (a)=>{return a*2} a=>a*2

Lexical Scope and Closure … function foo(){ let value=10; return () => {console.log(value++);} } var bar1 = foo(); var bar2 = foo(); bar1(); // ?? bar2(); // ??

… Lexical Scoping and Closure Inner function has access to the local variables of the outer function Lexical Scoping – the location in the source code where a variable is declared is used to determine where the variable is available Functions in JavaScript form closures. A closure is the combination of a function and the lexical environment within which that function was declared.

Function and Object Constructor Method A function invoked with the new keyword When invoked with new, the implicit parameter this is bound to a new object, and the function returns this object (unless there’s an explicit return statement) Method An object property that has a function value this in a method refers to the object the method is called on

Example: Constructor function Car( make, model, year ) { this.make = make; this.model = model; this.year = year; } var car1 = new Car("Honda", "Civic", 2016); var car2 = new Car("Ford", "F-150", 2017);

Creating Objects Creating single object Object literal new Object() – more verbose than using object literal Creating objects using constructor Creating objects from an existing object (i.e. prototype) using Object.create()

Example: Object.create() var car3 = Object.create(car1); console.log(car3.make); console.log(car3.model); console.log(car3); Prototype inheritance – an object inherits the properties of its prototype

Object Method Example let user = { name: 'Joe', hello: function() { console.log(`My name is ${this.name}`); } }; let user = { name: 'Joe', hello() { console.log(`My name is ${this.name}`); } };

Getters and Setters: Accessor Properties let user = { firstName: 'John', lastName: 'Doe', get name(){ return this.firstName + ' ' + this.lastName; }, set name(value){ [this.firstName, this.lastName] = value.split(' ') } };

Error and Exception Handling … The usage of try, catch, finally, throw are all similar to Java try { // code... } catch (err) { // error handling } Why not catch (var err) ??

… Error and Exception Handling The Error object Properties: name, message, and (maybe) stack Try/catch doesn't quite work with asynchronous code

Important JavaScript Functions Array functions, e.g. push, pop, slice, concat, indexOf, forEach JSON functions JSON.stringify and JSON.parse … The lodash library

Readings The Modern JavaScript Tutorial Eloquent JavaScript by Marijn Haverbeke Speaking JavaScript by Axel Rauschamyer MDN JavaScript Reference Node.js API Documentation