Download presentation
Presentation is loading. Please wait.
Published byLorena Roberts Modified over 9 years ago
1
JavaScript Functions & Objects
2
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()
3
encodeURI(), decodeURI() var uUri = "http://www.google.com.tw/search?q= 暨南大學 "; var eUri = encodeURI(uUri); document.write(uUri + " "); document.write(eUri + " "); document.write(decodeURI(eUri) + " "); http://www.google.com.tw/search?q= 暨南大學 http://www.google.com.tw/search?q=%E6%9A%A8%E5%8D%97%E5%A4%A7%E5%AD%B8 http://www.google.com.tw/search?q= 暨南大學
4
isNaN() window.onload = function() { document.getElementById(ageBtn).onclick=chkAge; }; function chkAge() { var age = document.getElementById(age).value; if (isNaN(age)) { alert(" 年齡輸入錯誤 !"); //... } Age: http://ycchen.im.ncnu.edu.tw/www2011/lab/isNaN.html
5
JavaScript Objects Boolean Number String Array Math Date RegExp RegExp
6
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");
7
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
8
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 )
9
indexOf() var email ="ycchen@ncnu.edu.tw"; posAt = email.indexOf("@"); if (posAt == -1) alert("Wrong E-mail!"); else { user = email.substring(0, posAt); serv = email.substring(posAt+1, email.length); alert("User name: "+user+"\nMail Server: "+serv); } http://ycchen.im.ncnu.edu.tw/www2011/lab/indexOf.html
10
slice( ) vs. substring( ) str = "Hello happy world! " 012345678901234567 876543210987654321 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
11
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"
12
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()
13
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]; // … }
14
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
15
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; }
16
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
17
Math Object Math is a static object. var area = 2*2* Math.PI; Properties: PropertyDescription EReturns Euler's constant (approx. 2.718) LN2Returns the natural logarithm of 2 (approx. 0.693) LN10Returns the natural logarithm of 10 (approx. 2.302) LOG2EReturns the base-2 logarithm of E (approx. 1.442) LOG10EReturns the base-10 logarithm of E (approx. 0.434) PIReturns PI (approx. 3.14159) SQRT1_2Returns the square root of 1/2 (approx. 0.707) SQRT2Returns the square root of 2 (approx. 1.414)
18
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
19
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]
20
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:00 1978 new Date ( value ) var day1970 = new Date(0); // Jan 1 00:00:00 1970 new Date( datestring ) var someday = new Date("Mar 29 13:01 2009");
21
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()
22
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()
23
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
24
Date Examples Date methods http://ycchen.im.ncnu.edu.tw/www2011/lab/date.html 日期時間計算 http://ycchen.im.ncnu.edu.tw/www2011/lab/date1.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.