SEEM4570 Tutorial 05: JavaScript as OOP

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
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
EIW: Javascript the Language1 The JavaScript Language.
1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
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.
Tutorial 11 Working with Operators and Expressions
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.
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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock.
INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1.
Tutorial 11 1 JavaScript Operators and Expressions.
Expressions and Data Types Professor Robin Burke.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Java Script Date Object
Introduction to Calculated Columns Variables, Conditionals, and String Manipulation PRESENTER: Cameron Blashka| Informer Implementation Specialist| April.
JavaScript.
Chapter 11 - JavaScript: Arrays
CGS 3066: Web Programming and Design Spring 2017
11 JavaScript: Objects.
>> Introduction to JavaScript
CHAPTER 10 JAVA SCRIPT.
Chapter 6 JavaScript: Introduction to Scripting
Chapter 4 Client-Side Programming: the JavaScript Language
In this session, you will learn about:
JavaScript Objects.
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
Week#2 Day#1 Java Script Course
JavaScript and Ajax (Expression and Operators)
Web Systems Development (CSC-215)
Objectives Create an array Work with array properties and methods
Perl Variables: Array Web Programming.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
Tutorial 4 JavaScript as OOP Li Xu
University of Kurdistan
JavaScript CS 4640 Programming Languages for Web Applications
The <script> Tag
Javascript data structures
PHP an introduction.
CIS 136 Building Mobile Apps
Strings and Dates in JavaScript
CGS 3066: Web Programming and Design Spring 2016
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
JavaScript Session III
JavaScript CS 4640 Programming Languages for Web Applications
Chapter 5 JavaScript Numbers and Expressions
Presentation transcript:

SEEM4570 Tutorial 05: JavaScript as OOP Cao Yuewen ywcao@se.cuhk.edu.hk 2017.10.26

JavaScript and Objects Technically, ”Everything” in JavaScript is Object Array, String, Number, … An object contains properties and methods Property has a name and value Methods are actions that can be performed on the object

JavaScript Data Type Class Description String To manipulate text strings Number To manipulate numbers Operators To assign values, compare values, perform arithmetic operations … Statements “Instructions" to be "executed" by the web browser in HTML Math To manipulate calculations Date Retrieves and manipulates dates and times Array Creates new array objects Boolean Creates new Boolean objects Error Returns run-time error information RegExp Finds and replaces characters in text strings Global Represents global variables and contains various built-in JS methods Conversion Converts different JS values to Number, String, Boolean

String Case sensitive String Methods: s.length; s.indexOf(“to”) ; s.slice(3,7); s.replace(“CUHK”,”HongKong”); s.toLowerCase(); s.concat(" ", "Campus"); s.charAt(1); …… var s = “Welcome to CUHK”

Number Numbers can be written with scientific notation e.g. var y = 123e-5; Number Methods Number.isFinite(x) ; Number.isInteger(x); Number.isNaN(x); x.toPrecision(3); x.toString(); ……. var x = 2.017;

Statements JavaScript statements often start with a statement identifier while (i < 10){ … } if (i < 10){ … } else if{ } else{ } for (i =0; i < 10; i++ ){ … }

Statements try { switch (i){ … //code to try to run case 1: } … catch(error){ …// code to handle errors finally{ … // code executed regardless of the try / catch result switch (i){ case 1: … break; case 2: default: }

Math Math object allows you to perform mathematical tasks. Math methods: Math.PI; Math.pow(2,3); Math.sqrt(64); Math.abs(-3.17); Math.ceil(3.17); Math.floor(3.17); Math.random(); Math.max(0,10,6,55); ……

Date Date object works with dates and times Four ways to instantiate a date: var d = new Date(); var d = new Date(milliseconds); //zero time is 01 Jan 1970 00:00:00 UTC var d = new Date(dateString); var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

Date Methods …… d.getDate(); //get day as a number(1-31) d.getFullYear(); //get the four digit year(yyyy) d.setDate(15); //set day as a number(1-31) d.setFullYear(2100,0,14); //sets a date object to a specific date Date.parse("March 21, 2012"); // returns the number of milliseconds between the date and January 1, 1970 ……

Array Array object is used to store multiple values in a single variable. e.g. var fruits = [“banana”,”apple”,”orange”]; Array indexes are zero-based. e.g. fruits[0] = “banana”; Array elements can be objects, like functions or arrays. e.g. myArray[0] = Date.getDay(); myArray[1] = fruits;

Array Methods Popping and Pushing Shifting Elements Deleting Elements x=fruits.pop(); // removes the last element, return the last element y=fruits.push(“Mango”); // adds element “Mango” to array fruits, return the length of new array Shifting Elements x=fruits.shift(); // removes the first element, return the first element. y=fruits.unshift(“Lemon”); // add element “Lemon” to array fruits, return the length of new array Deleting Elements delete fruits[1]; // change the second element in fruits to undefined Using delete may leave undefined holes in the array. Use pop() or shift() instead. var fruits = [“Banana”,”Apple”,”Orange”];

Array Methods Splicing an Array Merging Arrays var fruits = [“Banana”,”Apple”,”Orange”]; var color = [“red”,”blue”]; var points = [21,14,17,61,8]; Splicing an Array fruits.splice(1,2,”Lemon”); The first parameter defines the position where to add new elements The second parameter defines how many elements should be removed The rest parameters define the new elements to be added Merging Arrays new_arr=fruits.concat(color); // concatenates array fruits and color Sorting an Array By default, sort() function sorts value as strings fruits.sort(); // sort an array alphabetically fruits.reverse(); // sort an array in descending order points.sort(function(a,b){ return a-b}); //numeric sort points.sort(function(a,b){ return b-a}); //numeric sort in descending order fruits don’t change here!!!

RegExp A regular expression is an object that describes a pattern of characters. RegExp is used to perform pattern-matching and “search-and-replace” function on text. Syntax : /pattern/modifiers; Modifier Description i Perform case-insensitive matching g Perform a global match (find all matches rather than the first match) m Perform multiline matching

RegExp In JS, search() and replace() are often used regular expressions search() : search for a match, and returns the position of the match replace() : returns a modified string where the pattern is replaced e.g. var str = " Welcome to CUHK! "; var n = str.search(/come/i); // n=3 var res = str.replace(/cuhk/i, ”HongKong"); RegExp Object test() : searches a string for a pattern, and returns true or false, depending on the result. e.g. var tag = /e/. test(" Welcome to CUHK!"); exec() : searches a string for a specified pattern, and returns the found text. If no match is found, it returns null. e.g. var tag = /come/.exec(" Welcome to CUHK!"); str doesn’t change here!!!

Conversion The table below shows the result of converting JS values to Numbe, String, and Boolean Original Value Converted to Number Converted to String Converted to Boolean false "false" true 1 "true" "0" "1" "20" 20 "twenty" NaN [ ] "" [20] [10,20] "10,20" ["ten","twenty"] "ten,twenty"

Resources https://www.w3schools.com/js/default.asp https://www.w3schools.com/jsref/default.asp

Question Time