JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded.

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 CS Client Side Scripting CS380 2.
Presentation 8: JavaScript continued Arrays and objects Internet Technology 1.
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Web Application Development Muhammad Ali Versonic Pte Asher Imtiaz Forman Christian College.
Strings in Java 1. strings in java are handled by two classes String &
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Java Strings in 10 minutes
Session 8 JavaScript/Jscript: Objects Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
Functions and methods A method is a function that is a member of a class FCL(Framework Class Library) provides a rich collection of classes and methods.
1 CS101 Introduction to Computing Lecture 38 String Manipulations (Web Development Lecture 13)
1 CS101 Introduction to Computing Lecture 35 Mathematical Methods (Web Development Lecture 12)
6. JavaScript Objects and Object- Oriented Programming (OOP)
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
2 Alerts and the If/Else Conditional Statement CONTINUED There's No Right Way to Do It There are, literally, a million ways to write any given script.
JavaScript Part 2 – Page 1 of 35CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: JavaScript Part 2.
Scripting Languages.
UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Native JavaScript in Hyperion Intelligence Unleashing the Power of Object- Oriented Programming.
Web Application Development Muhammad Ali.  The Problem  Changing the Content of Page Dynamically  Efficient Utilization of Resources.
1 JavaScript: Objects and Object Models October 25, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel,
Lecture 8: Javascript and the DOM..  Javascript programming:  Math functions (random numbers, powers, square roots, etc.)  String functions (substrings,
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
Javascript core objects. Review question Which of the following is TRUE concerning a variable declared globally? a. The variable cannot be accessed anywhere.
Unit 7 JavaScript Core Objects. Core Objects Core Objects are objects built right into the language. For a complete list of properties and objects available.
JavaScript For...In Statement The for...in statement loops through the elements of an array or through the properties of an object. Syntax for (variable.
JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded URI.
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.
CSE 154 LECTURE 17: JAVASCRIPT. Client-side scripting client-side script: code runs in browser after page is sent back from server often this code manipulates.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
Functions and Arrays. Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and.
資訊工程學系 3A 彭博涵 2011/03/26 1.  什麼是 JavaScript?  JS Basic  JS Object 2.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Advanced JavaScript Topics – Page 1 of 31CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Advanced JavaScript.
Internet & World Wide Web How to Program, 5/e. 2.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Java Script Dvijesh Bhatt. Web Page HTML defines Web sites content through semantic tags (headings, paragraphs, lists, …). CSS defines 'rules' or 'styles'
CSE 154 Lecture 6: Javascript.
1 CSE 373 Data Types and Manipulation; Arrays slides created by Marty Stepp © University.
CSS XSLT DTD JS. “How to display HTML elements” External Style Sheets can save a lot of work. External Style Sheets are stored in CSS files. id Selector:
Hazırlayan:Emin BORANDAĞ 2/17/ Numerik Değerler Kullanımı Integers are considered accurate up to 15 digits. Try it function myFunction() { var x.
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
JavaScript and Ajax (JavaScript Arrays) Week 5 Web site:
Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry.
CS 201 California State University, Los Angeles.  Various Mathematical Functions  Characters  Strings.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
ADOBE FLEX Action Script Basics. MXML. Language Basics Action Script 3.0.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
Chapter 4 Java Script – Part2. 2 Location object Properties Properties href – whole path will be displayed. ex: window.location.href ( see example 11)
Introduction to Calculated Columns Variables, Conditionals, and String Manipulation PRESENTER: Cameron Blashka| Informer Implementation Specialist| April.
LECTURE 7 (ETCS-308) Subject teacher : Ms. Gunjan Beniwal
BIL 104E Introduction to Scientific and Engineering Computing
SEEM4570 Tutorial 05: JavaScript as OOP
Java Script.
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
VBScript Session 10.
Presentation transcript:

JavaScript Functions & Objects

JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded URI component encodeURI()Encodes a string as a URI encodeURIComponent()Encodes a string as a URI component escape()Encodes a string eval()Evaluates a string and executes it as if it was script code isFinite()Checks if a value is a finite number Number()Converts an object's value to a number isNaN()Checks if a value is not a number parseFloat()Parses a string and returns a floating point number parseInt()Parses a string and returns an integer unescape()Decodes a string encoded by escape()

encodeURI(), decodeURI() var uUri = " 暨南大學 "; var eUri = encodeURI(uUri); document.write(uUri + " "); document.write(eUri + " "); document.write(decodeURI(eUri) + " "); 暨南大學 暨南大學

isNaN() window.onload = function() { document.getElementById(ageBtn).onclick=chkAge; }; function chkAge() { var age = document.getElementById(age).value; if (isNaN(age)) { alert(" 年齡輸入錯誤 !"); //... } Age:

JavaScript Objects  Boolean  Number  String  Array  Math  Date  RegExp RegExp

Boolean Object  Create Boolean objects with an initial value of false: var myBoolean=new Boolean(); var myBoolean=new Boolean(0); var myBoolean=new Boolean(null); var myBoolean=new Boolean(""); var myBoolean=new Boolean(false); var myBoolean=new Boolean(NaN);  Create Boolean objects with an initial value of true: var myBoolean=new Boolean(1); var myBoolean=new Boolean(true); var myBoolean=new Boolean("true"); var myBoolean=new Boolean("false");   var myBoolean=new Boolean("Richard");

Number Object var myNum=new Number(86);  Properties:  MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY  Methods:  toExponential(num)  toFixed(num)  toPrecision(num)  toString( ) var num = 5000; var n2E = num.toExponential(2); //5.00e+3 var numObj = 6000; var o2E= numObj.toExponential(1); //6.0e+3 var num2 = 3.456; var n2F = num2.toFixed(1); //3.5 var n2P = num2.toPrecision(3); //3.46

String Object var myStr=new String("Hello World!");  Properties: length  Methods: charAt( index ), charCodeAt( index ), concat( stringX, stringX,..., stringX ) fromCharCode( numX, numX,..., numX ) indexOf( searchvalue, fromindex ), lastIndexOf( searchvalue, fromindex ) match( searchvalue ), search( searchvalue ) replace( findstring, newstring ) toLowerCase( ), toUpperCase( ) slice( start, end ), substr( start, length), substring( start, stop) split( separator, howmany )

indexOf() var posAt = if (posAt == -1) alert("Wrong !"); else { user = .substring(0, posAt); serv = .substring(posAt+1, .length); alert("User name: "+user+"\nMail Server: "+serv); }

slice( ) vs. substring( ) str = "Hello happy world! " str.slice(6,13)  "happy w" str.substring(6,13)  "happy w" str.slice(6)  "happy world!" str.substring(6)  "happy world!" str.slice(13, 6)  "" str.substring(13,6)  "happy w" str.slice(-16, 8)  "llo ha" str.substring(-16,8)  "Hello ha"   ( length-index ) index

split( separator, howmany ) var str1 = "JavaScript, CSS, XML, Dynamic HTML"; var arr1 = str1.split(", "); arr1[0]arr1[1]arr1[2]arr1[3] JavaScript CSS XML Dynamic HTML var str2 = 'Content-type: multipart/mixed; boundary="----xxyy"'; var arr2 = str2.split(": ", 2); arr2[0]arr2[1] Content-type multipart/mixed; boundary="----xxyy"

Array Object var arr1 = new Array(); var arr2 = new Array(4); var arr3 = new Array(2009, "April", true);  Properties:length  Methods: concat( arrayX, arrayX,..., arrayX ) pop(), push( element1, element2,..., elementX ) shift( ), unshift( element1, element2,..., elementX ) slice( start,end ), splice( index, howmany, element1,..., elementX ) reverse( ), sort( sortbyfunc ) join( separator ) shift() unshift() pop() push() splice()

length var arr1=new Array(); var sum=0; arr1[0]= 32; arr1[1]=75; // … for (var i=0; i< arr1.length ; i++) { sum += arr1[i]; // … }

splice( index, howmany, element1,..., elementX ) var arr = [0,1,2,3,4,5,6]; arr.splice(2, 0, "a1", "a2", "a3"); document.write(arr+" "); arr.splice(2, 3); document.write(arr+" "); arr.splice(3, 1, "a1", "a2", "a3"); document.write(arr+" "); // 0,1,a1,a2,a3,2,3,4,5,6 // 0,1,2,3,4,5,6 // 0,1,2,a1,a2,a3,4,5,6

sort( sortbyfunc ) var arr = new Array(6); arr[0] = 10; arr[1] = 5; arr[2] = 40; arr[3] = 25; arr[4] = 1000; arr[5] = 1; document.write(arr + " "); // 10,5,40,25,1000,1 document.write(arr.sort()+ " ");// 1,10,1000,25,40,5 document.write(arr.sort(sortByNum)+ " ");// 1,5,10,25,40,1000 function sortByNum (a, b) { return a-b; }

join( separator ) var arr1 = ["JavaScript", "CSS", "XML", "Dynamic HTML"]; var str1 = arr1.join(); var str2 = arr1.join(", "); document.write(str1+" "); document.write(str2+" "); JavaScript,CSS,XML,Dynamic HTML JavaScript, CSS, XML, Dynamic HTML

Math Object  Math is a static object. var area = 2*2* Math.PI;  Properties: PropertyDescription EReturns Euler's constant (approx ) LN2Returns the natural logarithm of 2 (approx ) LN10Returns the natural logarithm of 10 (approx ) LOG2EReturns the base-2 logarithm of E (approx ) LOG10EReturns the base-10 logarithm of E (approx ) PIReturns PI (approx ) SQRT1_2Returns the square root of 1/2 (approx ) SQRT2Returns the square root of 2 (approx )

Math's Methods MethodDescription random() a random number r, 0 ≦ r < 1, i.e. r in [0,1) abs(x) the absolute value of a number max(x,y, …)max(x,y, …), min(x,y, …) min(x,y, …) the number with the highest, lowest value of x, y, … round(x) Rounds a number to the nearest integer ceil(x)ceil(x), floor(x)floor(x) rounded upwards, downwards to the nearest integer pow(x,y) the value of x to the power of y exp(x) the value of E x log(x) the natural logarithm (base E) of a number sqrt(x) the square root of a number cos(x)cos(x), acos(x)acos(x) the cosine, arccosine of a number sin(x)sin(x), asin(x)asin(x) the sine, arcsine of a number tan(x)tan(x), atan(x), atan2(y,x)atan(x) atan2(y,x) the tangent, arctangent of x, the angle theta of an (x,y) point

Math.random() var rand1 = Math.random(); // [0,1) var rand2 = myRandom(1, 10);// [1, 10) var rand3 = myIntRandom(1, 49);// integer in [1, 49] function myRandom(a, b) { return a+Math.random()*(b-a); } function myIntRandom(a, b) { return Math.floor(a+Math.random()*(b-a+1)); } // a random number in [a, b) // an random integer in [a, b]

Date Object  Constructors new Date( ) var today = New Date(); // Current date and time new Date ( year, month, date, hours, minutes, seconds, ms ) var birthDay = new Date(1978, 0, 31); // Jan 31 00:00: new Date ( value ) var day1970 = new Date(0); // Jan 1 00:00: new Date( datestring ) var someday = new Date("Mar 29 13: ");

Date's Methods (1/3) Methods Description getFullYear()the year, as a four-digit number getMonth()the month (from 0-11) getDate()the day of the month (from 1-31) getDay()the day of the week (from 0-6) getHours()the hour (from 0-23) getMinutes()the minutes (from 0-59) getSeconds()the seconds (from 0-59) getMilliseconds()the milliseconds (from 0-999) getTime()milliseconds since midnight Jan 1, 1970 getTimezoneOffset()Difference in minutes between local time and Greenwich Mean Time (GMT) getUTCFullYear(), getUTCMonth(), getUTCDate(), getUTCDay(), getUTCHours(), getUTCMinutes(), getUTCSeconds(), getUTCMilliseconds()

Date's Methods (2/3) Methods Description setFullYear()the year, as a four-digit number setMonth()the month (from 0-11) setDate()the day of the month (from 1-31) setHours()the hour (from 0-23) setMinutes()the minutes (from 0-59) setSeconds()the seconds (from 0-59) setMilliseconds()the milliseconds (from 0-999) setTime()milliseconds since midnight Jan 1, 1970 setUTCFullYear(), setUTCMonth(), setUTCDate(), setUTCHours(), setUTCMinutes(), setUTCSeconds(), setUTCMilliseconds()

Date's Methods (3/3) Methods Description toString()Convert to a string toDateString()The date portion in readable form toTimeString()The time portion in readable form toLocaleString()Convert to a string, according to local time toLocaleDateString()Convert to a string, according to local time, and returns the date portion toLocaleTimeString()Convert to a string, according to local time, and returns the time portion toUTCString()Convert to a string, according to universal time parse()Takes a date string and returns the number of milliseconds since midnight of January 1, 1970 UTC()Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time

Date Examples  Date methods  日期時間計算