CS5220 Advanced Topics in Web Programming JavaScript 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

Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
The Web Warrior Guide to Web Design Technologies
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.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
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.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
CP476 Internet Computing JavaScript Client-Side Programming 1 1. What is JavaScript –Another script language for both client-side and sever-side programming.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
SE-2840 Dr. Mark L. Hornick 1 Modifying webpage behavior using client-side scripting Javascript.
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.
1 Lecture #6 Dynamic Web Documents HAIT Summer 2005 Shimrit Tzur-David.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Martin Kruliš by Martin Kruliš (v1.0)1.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
JavaScript Syntax, how to use it in a HTML document
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.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Expressions and Data Types Professor Robin Burke.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
Click to add Text Javascript Presented by Bunlong VAN Basic.
Javascript Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
CGS 3066: Web Programming and Design Spring 2017
Build in Objects In JavaScript, almost "everything" is an object.
>> Introduction to JavaScript
JavaScript Introduction
“Under the hood”: Angry Birds Maze
Chapter 4 Client-Side Programming: the JavaScript Language
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
JavaScript is a programming language designed for Web pages.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
JavaScript Fundamentals
JavaScript Syntax and Semantics
JavaScript.
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
Web Systems Development (CSC-215)
WEB PROGRAMMING JavaScript.
PHP.
Functions, Regular expressions and Events
JavaScript What is JavaScript? What can JavaScript do?
Tutorial 4 JavaScript as OOP Li Xu
University of Kurdistan
CS5220 Advanced Topics in Web Programming Node.js Basics
CS3220 Web and Internet Programming JavaScript Basics
JavaScript What is JavaScript? What can JavaScript do?
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
PHP an introduction.
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CIS 136 Building Mobile Apps
CS3220 Web and Internet Programming JavaScript Basics
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
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

CS5220 Advanced Topics in Web Programming JavaScript Basics Chengyu Sun California State University, Los Angeles

Web Development Server-Side Client-Side C, Perl Java C#, VB JavaScript PHP ColdFusion Ruby Python JavaScript … JavaScript Applet JavaFX Flex/Flash Silverlight HTTP Browser What do Applet, JavaFX, Flex/Flash and Silverlight have in common? Application Server

Why Client-Side? Improve user experience Reduce server workload Interactive Responsive Reduce server workload Handle input events Implement as much functionality on the client-side as possible Hide the inevitable communication overhead from the user

About JavaScript Originally developed by Netscape Standardized as ECMAScript ECMAScript 5 is fully supported by all modern browsers Influenced by Java and various scripting languages Web Server Client-side Core Server-side Browser

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

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

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

Types Boolean Number String Null Undefined Object Primitive Types

Literals Boolean: true, false Number: 123, 4.56 String: “hello”, ‘world’ Null and Undefined: null, undefined Object literal

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 property:value pairs

JSON (JavaScript Object Notation) Used as a data exchange format Based on a subset of JavaScript syntax String are double quoted Property name 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??

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

Variables var x; // declare a variable x x = 10; // now x is a number x = ‘abc’;// now x is a string y = 20; // y is a global variable // (a property in the global object) JavaScript variables are dynamically typed (think of them as references instead of storage spaces)

Variable Scope var a = 10; b = 20; function foo() { var c = 0; for( i=1 ; i < 3 ; ++i ) c += i; return a + b + c; } foo(); // ?? console.log(a, b, c, i); // ??

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 1 == true “” == false null == false undefined == false 0 == “” null == undefined Pros and cons of automatic type conversion??

Statements All Java statement, e.g. if, for, while, switch … Semicolon is optional but highly recommended

Example: Object “Deep” Comparison Write a function deepEqual(obj1, obj2) to compare two objects by value typeof and in operator for(property in object) statement Function and recursion

Functions as First-class Citizens In JavaScript, functions are actually objects Assigned to a variable Assigned as a property of an object Function literals (i.e. functions without names) 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], …

Lexical Scope and Closure … function foo(){ var value=10; return function(){ return 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); car1 instanceof Car; // ??

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); Difference between car3 and car1? Prototype inheritance – an object inherits the properties of its prototype

Common JavaScript Functions IO functions, e.g. console.log, alert, confirm, prompt Array functions, e.g. push, pop, slice, concat, indexOf, forEach JSON functions JSON.stringify and JSON.parse …

Readings Speaking JavaScript by Axel Rauschmayer Eloquent JavaScript by Marijn Haverbeke - http://eloquentjavascript.net/ MDN JavaScript Reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference